Maxwell Chandler
Maxwell Chandler

Reputation: 642

updateSelectInput causing all of reactive code in Shiny Server to re-run

I have a basic server structure that looks something like this

server <- shinyServer(function(input, output, session) {
library(readr)
data <- reactive({req(input$file1)
inFile <- input$file1 
df <- as.data.frame(read_csv(inFile$datapath)) # read in data

updateSelectInput("do stuff") #change what happens in the plot based on selection


return(df)
  })

output$priceCurve <- renderPlot({
 "do plot stuff"  #plot stuff based on upDateSelectInput above

})
})

What happens is I ask the user for a csv. They upload one. Then based on a selection they make I subset the data frame produced by the csv and plot it.

The problem is that every time the user selects a drop down option the app re reads the csv. It shouldn't have to do that more than once. I want it to just store it in memory and do operations on the data frame based on the selection. I don't want it to read in the data every single time a selection is made.

Upvotes: 0

Views: 120

Answers (1)

CPak
CPak

Reputation: 13581

Tough to see if this will work without a reproducible example but try this

Wrap your read file statements in isolate(). This should make these statements depend only on input$file1 (I think), but data will still be reactive to updateSelectInput("do stuff") (I think).

server <- shinyServer(function(input, output, session) {
    library(readr)
    data <- reactive({
        isolate(req(input$file1)
                inFile <- input$file1 
                df <- as.data.frame(read_csv(inFile$datapath))
                ) # read in data

        updateSelectInput("do stuff") #change what happens in the plot based on selection

        return(df)
    })

    output$priceCurve <- renderPlot({
                            "do plot stuff"  #plot stuff based on upDateSelectInput above
                        })
})

Upvotes: 1

Related Questions