Seems like a simple problem, yet stick - editing data.frame within shinyServer

22 views
Skip to first unread message

Dev Dash

unread,
Sep 1, 2015, 1:02:11 AM9/1/15
to Shiny - Web Framework for R
I cannot figure out why I keep getting [operation not allowed without an active reactive content]

shinyServer(function(input,output,session){
  
  df <- reactiveValues(f = data.frame(1,1,1))
  list <- reactiveValues()
  list = c("VTI","ARR","PJP")
  for (i in 1:length(list))
  {
   getSymbols(list[i])
    print(i)
    print(list[i])
    df$f[i,1] = list[i]

  }
  
  output$table1 <- renderTable({df$f})
  
  
})

Thanks

Joe Cheng

unread,
Sep 1, 2015, 6:29:50 PM9/1/15
to Dev Dash, Shiny - Web Framework for R
A couple of things here.

First:


  list <- reactiveValues()
  list = c("VTI","ARR","PJP")

<- and = are equivalent here. So you're assigning reactiveValues() to list but then immediately overwriting it with c("VTI", "ARR", "PJP"). (BTW it's probably not a great idea to have a variable named "list" anyway, given how fundamental and commonly used the list() function is in R.)

Second, I wouldn't store the data frame as a reactive value. Instead I would calculate the data frame as a reactive expression.

Third, you can't read from reactiveValues (or access the values of reactive expressions) unless you are executing within a reactive(), or an output's render expression, or in an observe() or observeEvent(). The reason for this is that if you're outside all of those contexts, then you're just executing code once, before any user interaction happens. It generally doesn't make sense to read from reactive values but then not to have a way to re-execute if those reactive values change.

Rewritten:

shinyServer(function(input,output,session){
  values <- reactiveValues(symbols = c("VTI", "ARR", "PJP"))

  df <- reactive({
    results <- new.env(parent = emptyenv())
    # Be careful not to let getSymbols assign into .GlobalEnv. That could get
    # really confusing if multiple sessions are active in this Shiny app at once.
    getSymbols(values$symbols, env = results, auto.assign = TRUE)

    # I didn't know how you wanted to make the data frame, but it goes here
    data.frame(Symbol = values$symbols, ...)
  })

  output$table1 <- renderTable({ df() })
})


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/349807cb-f76d-460b-9928-460c1dc7f1c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages