D.Hadley D.Hadley - 4 days ago 5
R Question

How to update default csv data with action button in Shiny?

My app takes a long time to calculate, so on the first load, I would like shiny to use default data saved as a csv file. I would, however, like users to be able to update this data (and the resulting plots) by pushing an action button. I have tried a few configurations. Here is what a simplified version looks like with observeEvent.

server.R



default_data <- read.csv("./default_data.csv")

function(input, output) {

# Load default
data <- reactive(default_data)

# I wish this could get it to update
observeEvent(input$goButton, {
data <- rnorm(100, input$recid_rate)
})

# Plot
output$plot <- renderPlot({

hist(data())

})

}


I have it to where the default data loads and is visulized in ggplot. But action buttons do not seem to override the default data. Thanks in advance for your thoughts!

Answer

Your problem is that data is supposed to respond to 2 different reactive statements and does not know what to do. Here is a related question.

1 way to fix this to to make a reactiveValues object that holds the data:

rv<-reactiveValues(data=default_data)

Then update it with the observeEvent:

 observeEvent(input$goButton, {
    rv$data <- rnorm(100, input$recid_rate)
  })

and the use it for the plot:

 output$plot <- renderPlot({
    hist(rv$data)
  })
Comments