Data not available outside of observeEvent() statement?

4,142 views
Skip to first unread message

lc201203

unread,
Oct 15, 2016, 11:59:31 AM10/15/16
to Shiny - Web Framework for R
I have run into what seems like a very simple simple issue in Shiny but I cannot figure out what the issue is.

Basically, I want to set an observer to plot data that is generated by a separate observeEvent() statement. Consider the following app:

ui <- (fluidPage(
  actionButton("button","Press Me"),
  verbatimTextOutput("data")
))

server <- function (input,output) {
  observeEvent(input$button,{
    x <- 1
  })

  observe({
    output$data <- renderPrint(x)
  })
}

shinyApp (ui=ui,server=server)

Naturally, upon startup of the app there is an error message stating "object 'value' not found" since it has not been assigned a value on startup. However, when pressing the "Press Me' button I would expect that 'x' be assigned a value of 1, to which the observer would then print into the 'data' output. Instead, it continues to show the same error message.

If I modify the server to read as follows, it works:

server <- function (input,output) {
  observeEvent(input$button,{
    value <- 1
    output$data <- renderPrint(value)
  })
}

Why are variables assigned within an observeEvent statement (seemingly) only available within the statement itself?

Joe Cheng

unread,
Oct 15, 2016, 11:27:12 PM10/15/16
to lc201203, Shiny - Web Framework for R
Like functions, observers create their own variable scope. By default, variable writes are local to each function/observer.

If you want to make x available to both the observer and the renderPrint, you'd need to declare "x <- NULL" outside of the observer (but still inside the shiny server function) and then assign to it from inside the observer with the <<- operator, e.g. "x <<- 1".

But simple variables do not trigger reactivity--assigning to x will not, by default, cause renderPrint to re-run. You need to specifically tell Shiny to treat x as a reactive variable, by calling makeReactiveBinding.

I haven't tried this but it should work:

server <- function (input,output) {
x <- NULL
makeReactiveBinding("x")


observeEvent(input$button,{
x <<- 1
})

observe({
output$data <- renderPrint(x)
})
}

Further reading:
http://adv-r.had.co.nz/Functions.html#lexical-scoping
http://shiny.rstudio.com/articles/scoping.html

--
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/57b89b86-7ff1-4b97-ac0f-6010c0313aa7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Xing Chen

unread,
May 13, 2020, 9:12:40 PM5/13/20
to Shiny - Web Framework for R
Hi, 

Based on your post, is there any trick to assign multiple variables as NULL?  What I need is :


server <- function (input,output) {

   x <- NULL
   y <- NULL
   ...
   z <- NULL

  observeEvent(input$button,{
    x <<- 1
    y <<- 1
     ...
    z <<- 1
  })
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages