Re: Shiny error: Operation not allowed without an active reactive context

598 views
Skip to first unread message

Winston Chang

unread,
Aug 19, 2013, 9:59:07 PM8/19/13
to shiny-...@googlegroups.com
The subset function has non-standard evaluation, which can cause problems like this, so it's better to use `[` for indexing into the data structure.

From ?subset:
"This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences."

-Winston



On Mon, Aug 19, 2013 at 8:16 PM, Environmetrics <environ...@gmail.com> wrote:
I'm new to Shiny and can't figure out why the code below generates the    Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive function.)   message:

All I want to do is subset a dataframe based on user-selected value of "site".

shinyServer(function(input,output) {
   
    siteInput<-reactive({
    return(input$site)
    })
   
dataSubset<-reactive({
    subset(NTU,NTU$site==siteInput())


})




--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kevin Davenport

unread,
Aug 20, 2013, 2:06:24 PM8/20/13
to shiny-...@googlegroups.com
Hey guys, I understand the subset considerations inside Shiny from search through older posts but I'm having some difficulty figuring out the best way to subset a data frame on the fly with dynamically populated drop-downs ( something like options = as.list(levels(dat$Round)) )

This is a basic representation of what I have been doing for just selecting the data frame:

Global.R
Client1        <- read.csv("data/client1_audits.csv", header = TRUE, fill = FALSE) 
Client2        <- read.csv("data/client1_audits.csv", header = TRUE, fill = FALSE) 
Client3        <- read.csv("data/client3_audits.csv", header = TRUE, fill = FALSE) 

data_sets <- c("Client1", "Client2", "Client3") #call from UI

Server.R
shinyServer(function(input, output) {
  
  # get data from .csv defined in global.R
  inputVariable <- reactive({
    input$client_select
  })

  # ddply reactive
  reactive.auditorPlyr <- reactive({
    if(is.null(input$client_select))
      return()
    dat <- get(input$client_select) 
    ddply(dat, .(Auditor), summarize, 
          n            = length(Score),
          avgSco       = round(mean(Score),2),
          mScore       = round(median(Score),2),
          sdScore      = round(sd(Score),2),
          seScore      = round(sd(Score) / sqrt(length(Score)),2),
          avgDuration  = round(mean(Duration/60),2),
          mDuration    = round(median(Duration/60),2),
          sdDuration   = round(sd(Duration/60),2),
          seDuration   = round(sd(Duration/60) / sqrt(length(Duration/60)),2)                   
    )
  })
  
  
  output$auditorPlyr <- renderGvis({
    gvisTable(reactive.auditorPlyr() )         
  })

UI.R

 sidebarPanel(
    
    wellPanel(
      selectInput("client_select", "Client:", as.list(data_sets)) # ,
      #selectInput("city_select", "City:", choices = as.list(levels(dat$City)))

      )

I'm creating the dataframe "dat" based off the single UI input from the globally defined csvs. What I would like to do is subset dat further via a few more drop downs that would fill based on the factors/levels of a column. For example a "city" dropdown would be populated by the entries that exist in the city column of the dataframe selected in dropdown 1 (client select). Then when a specific city is selected from the drop down the client dataframe is subsetted to only include observations where city == "xyz".

Thank you  

Zhuo Jia Dai

unread,
Aug 21, 2013, 1:10:31 AM8/21/13
to shiny-...@googlegroups.com
#selectInput("city_select", "City:", choices = as.list(levels(dat$City)))

By looking at your commented out code I think I sort of understand what you trying to do. Unfortunately (correct me if I am wrong) ui.R only gets run ONCE per session, it is run at the beginning to generate how the page looks like initially. So to update the controls after you selected another input you will need to look for how to use updateSelectInput. Check the tutorial, there's should be one page on dynamic UI that teaches you how to do it. 

Kevin Davenport

unread,
Aug 21, 2013, 11:23:29 AM8/21/13
to shiny-...@googlegroups.com
Hi Zhuo thanks for your reply. I figured something out using renderUI here: https://groups.google.com/forum/#!topic/shiny-discuss/PDPWKRV_lSU
I'm not sure if this was an elegant way to implement it though. I can't get the the group checkbox to work right. 
Reply all
Reply to author
Forward
0 new messages