I am trying to render some output plots in a Shiny app but need input values to change a "global" data structure (aka I need the data to be accessible to render multiple plots).
This is a simplified version of my app
ui <- fluidPage(
fluidRow(
selectInput("target_state", NULL, choices=setNames(tolower(state.abb), state.name), selected='pa')
),
fluidRow(
column(plotOutput("plot1", height="220px")),
column(plotOutput("plot1", height="220px"))
)
server <- function(input,output) {
output$plot1 <- renderPlot({## code to generate plot here})
output$plot2 <- renderPlot({## code to generate another plot here})
}
I want to load specific data sets based on the input of target_state
. I have tried the following:
data_in <- reactive({
in_file <- read.csv(paste0("datafile.",input$target_state,".csv")
as.data.frame(in_file)
})
Then tried referencing this as both a data structure (as data_in
) and as a function (`data_in()')
This loads fine initially, but the plots do not change when I select different input values. And I am trying to avoid reloading the data everytime I need it. How can I go about dynamically loading a "global" dataset in my Shiny app?