Problem

The plot is only generated when we click the plot button. However, since the main argument depends on the slider value, the plot title is wrong.

sliderInput("n", "Sample size", 10, 50, 10)
selectInput("dist", "Distribution", c("Normal", "Uniform"))
actionButton("go", "Plot it!")

rv = reactiveValues(data = rnorm(100))
observeEvent(input$go, {
  if(input$dist=="Normal") rv$data = rnorm(input$n)
  else rv$data = runif(input$n)
})

## The plot title changes with the slider.
renderPlot({plot(rv$data, main=paste("Sample size: ", input$n))})

Solution

Use isolate to remove input$n from the dependency tree.

renderPlot({plot(rv$data, 
                 main=paste("Sample size: ", isolate(input$n)))})


jr-packages/jrShiny documentation built on Feb. 16, 2020, 9:13 p.m.