Update data for output only when both an input changes and a button is pressed

I have a somewhat complicated app which queries APIs and databases.

The user inputs some values, some of which change the data and thus require calling the API again, while others only change how the data is manipulated.

The whole thing is kicked off by an action button.

I would like to only update the data when both the action button is pressed, and the input relating to the data is changed. If the input relating to the data is not changed, I want to keep the data as is.

The following example captures what I want. a solution would mean that if you change n, the plot data changes when you press go. But if n stays the same, and you change the color (and press Go), only the color changes and not the underlying data.

Any help would be greatly appreciated!

# Global variables can go here
n <- 200

# Define the UI
ui <- bootstrapPage(numericInput('n', 'Number of obs', n),
                    numericInput('color', "Numeric code for color", 0),
                    actionButton("go", "Go!"))

server <- function(input, output) {
  # This gathers the data - I want it to stay the same unless I change input$n and click go
  data <- eventReactive(input$go,
  color <- eventReactive(input$go,
  output$plot <- renderPlot({
    hist(data(), col = color())

# Return a Shiny app object
shinyApp(ui = ui, server = server)

I found a solution which works great, but it does require another package.

Using the memoise package, I can replace the runif function with a memoised version. This caches the value of the function and only reruns it when the input changes.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.