where can you use input value in shiny?

According to Chapter 3 Basic reactivity | Mastering Shiny and my previous experience the below code should throw an error like

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.)

However, it does not and seems to work without a problem. My problem is that now I am confused if I understand reactivity or not. (I still get the error if I try to use input from not in a function.

library("shiny")

ui <- fluidPage(
  checkboxInput("test", "test"),
  textOutput("text")
)

server <- function(input, output) {
  # this throws error as expected
  # message(input$test)

  test_value <- function() {
    input$test
  }

  output$text <- renderText({
    test_value()
  })
}

shinyApp(ui = ui, server = server)

Probably I am missing something. Thanks!

I make no claim to being a shiny expert but I think you can do this because you are using test_value() inside of a reactive environment. You define test_value() outside of a reactive environment but it is used inside of the renderText(). If you add a line

message(test_value())

you will get the expected error.

Edit: I see this as an aspect of lazy evaluation, though maybe that is not the appropriate term here. input$test is not evaluated during the function definition but onlywhen you actually call for it.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.