I have two input selection and an action button to generate a plot and download the data. I would like to clear the output contents (plot and download button) any time there is a change in the input selection. The code below will only clear the plot and not the download button. Not sure if the reactiveValues under the downloadhandler is correct.
library(shiny)
library(ggplot2)
library(openxlsx)
ui = fluidPage(
textInput("textT", label = "Title", value = ""),
textInput("textX", label = "X-Axis Label", value = ""),
actionButton("Btn", "Run", icon=icon("play-circle")),
plotOutput('plot1'),
conditionalPanel(condition = "input.Btn>0", downloadButton("dwload", "Download"))
)
server = function(input, output, session) {
v <- reactiveValues(clearAll = TRUE)
observeEvent(c(input$textT, input$textX), {
v$clearAll <- TRUE
}, priority = 10)
observeEvent(input$Btn, {
output$plot1 = renderPlot({
if (v$clearAll)
return()
else
ggplot(mtcars, aes(x= gear, y= carb)) + geom_line() +ggtitle(input$textT) + xlab(input$textX)
})
output$dwload <- downloadHandler(
filename = function() {
paste0("Checks-", gsub(" ", "_", gsub(":", ".", Sys.time())), ".xlsx")
},
content = function(file) {
if (v$clearAll)
return()
else
quick_xlsx(mtcars, file=file)
}
)
v$clearAll <- FALSE
}, priority = 10)
}
shinyApp(ui, server)
I'd appreciate any help.
Thank you!