rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.87k forks source link

Give more information when using reactives incorrectly #2480

Open hadley opened 5 years ago

hadley commented 5 years ago
library(shiny)
ui <- fluidPage(
  numericInput("count", label = "Number of values", value = 100)
)
server <- function(input, output, session) {
  message("The value of input$count is ", input$count)
}
# Current
#>  Error in `$<-.reactivevalues`(`*tmp*`, count, value = 10) : 
#>  Attempted to assign value to a read-only reactivevalues object
# Suggested
#> Error: Can not assign value to read-only reactiveValue `count`
server <- function(input, output, session) {
  message("The value of input$count is ", input$count)
}

shinyApp(ui, server)
# Current
#> Error in .getReactiveEnvironment()$currentContext() : 
#>  Operation not allowed without an active reactive context. (You tried to do
#>  something that can only be done from inside a reactive expression or observer.)
# Suggested
#> Error: Can not access reactive value `count` outside of a reactive context.
#> Do you need to wrap inside a reactive() or observer()?
daattali commented 5 years ago

Funny enough, whenever I teach shiny, I always explicitly say how much I love that accessing a reactive outside of a reactive context is a great example of informative error messages, because it does actually tell you the problem.

Though I can see how .getReactiveEnvironment()$currentContext() can be intimidating to newcomers, and I agree it would be helpful to see the name of the variable that was attempted to be accessed