My shiny application allows the user to upload a time series data file, and then select which variables to plot (ggplot) using a checkboxGroupInput
. When I load a new file, I want to clear the checkbox selections and just draw an empty plot until the user selects variables again. Unfortunately, what actually happens is the plot redraws briefly using the old selection, then clears the selections and draws the empty plot. I have a reprex below, to reproduce, load file1.csv, choose a variable to plot, and then load file2.csv, you'll see the different data from file2 pop on the screen briefly and then disappear.
I'd appreciate any tips here... I feel this is a simple reactive thing but everything I've tried hasn't worked. I'm not sure if it's complicating things, but I have the plot and checkbox hidden in a conditional panel until the file is loaded... my real application has more stuff that is hidden as well. Thanks in advance for any help on this particular issue, or any tips you have about my code in general!
library(shiny)
library(tidyverse)
# create example data files to use for loading
write_csv(data.frame(time=1:10,var1=sample(1:10),var2=sample(1:10)),"file1.csv")
write_csv(data.frame(time=1:10,var1=sample(1:10),var2=sample(1:10)),"file2.csv")
ui <- fluidPage(
fileInput("logfile", "Choose data file", multiple = FALSE),
conditionalPanel("output.fileUploaded",
plotOutput('plot'),
checkboxGroupInput("variables",label="Variables to Plot")
)
)
server <- function(input, output, session) {
df <- reactive({
req(input$logfile)
read_csv(input$logfile$datapath) %>% pivot_longer(-time)
})
output$fileUploaded <- reactive({
updateCheckboxGroupInput(session, "variables", "Variables to show",
unique(df()$name))
return(!is.null(df()))
})
outputOptions(output, 'fileUploaded', suspendWhenHidden=FALSE)
output$plot <- renderPlot({
ggplot(filter(df(),name %in% input$variables),aes(x=time,y=value)) +
geom_line(aes(col=name))
})
}
shinyApp(ui = ui, server = server)