In my Shiny App, there are two inputs: the number of observations (an integer), and the color (a character to be choosen between red, green and blue). There is also a "GO!" action button.
Which Shiny function to use in order to:
- have the random numbers regenerated and the histogram updated with this new data only when the user click on the "Go!" button.
- be able to change the color of the histogram on the fly without regenerating the random numbers.
I would prefer a solution that provides the maximum clarity to the code.
See below one of my unsuccessful tentative with isolate.
# DO NOT WORK AS EXPECTED
# Define the UI
ui <- fluidPage(
sliderInput("obs", "Number of observations", 0, 1000, 500),
selectInput('color', 'Histogram color', c('red', 'blue', 'green')),
actionButton("goButton", "Go!"),
plotOutput("distPlot")
)
# Code for the server
server <- function(input, output) {
output$distPlot <- renderPlot({
# Take a dependency on input$goButton
input$goButton
# Use isolate() to avoid dependency on input$obs
data <- isolate(rnorm(input$obs))
return(hist(data, col=input$color))
})
}
# launch the App
library(shiny)
shinyApp(ui, server)
