0

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!

D.Hadley
  • 1,179
  • 13
  • 13

1 Answers1

1

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)
  })
Community
  • 1
  • 1
John Paul
  • 12,196
  • 6
  • 55
  • 75