After rewriting this into a reprex (below), I no longer think this makes a particularly good case study because it depends on a specific behaviour of data.table which won't be familiar to that many readers of the book.
library(shiny)
library(data.table)
mtcars <- as.data.table(mtcars)
ui <- fluidPage(
fluidRow(
column(3, tagList(
selectInput("cyl", "Cylinders", choices = mtcars$cyl),
uiOutput("vs"),
textOutput("mpg")
))
)
)
server <- function(input, output) {
output$vs <- renderUI({
selectInput("vs", "VS", mtcars[cyl == input$cyl, vs])
})
mpg <- reactive({
mtcars[cyl == input$cyl & vs == input$vs, mpg]
})
output$mpg <- renderText({
mean(mpg())
})
}
shinyApp(ui, server)
The use of a render function is critical here, otherwise the problem is never triggered because nothing forces the reactive to evaluate.
(In this case I'd also recommend using updateSelectInput() instead of renderUI(); it doesn't solve the problem but it does make the code simpler)