Problem

Each plot uses the slider value to generate random numbers. However,

sliderInput(inputId = "num1", label = "Sample size",
            value = 25, min = 1, max = 100)

## Both plots get different data!!
## rnorm has been executed twice!
renderPlot({
  x = rnorm(input$num1)
  plot(x)
})

renderPlot({
  x = rnorm(input$num1)
  plot(x)
})

Solution

Create a reactive object that each plot can access.

x = reactive(rnorm(input$num1))
renderPlot({plot(x())})
renderPlot({plot(x())})


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