R:使用闪亮的应用程序重置情节的按钮

R: Button to reset a plot using shiny apps

如何重置图表以显示空白图?我已经创建了一个重置​​按钮并尝试了各种建议,但它们通常会导致某种问题或者它们什么都不做。

ui <- fluidPage(
    theme = shinytheme("cerulean"),
    navbarPage( "Unemployment Rate Comparison Tool",
                tabPanel("Interactive Graph",
                         titlePanel("US Unemployment Rates Before and After COVID-19"),
                         sidebarLayout(
                             sidebarPanel(
                                 selectInput(
                                     inputId = "y",
                                     label = "Select State(s) to Graph",
                                     choices = unique(q_long$State),
                                     selected = "United States",
                                     multiple = TRUE
                                 ), # select input end
                                 radioButtons(
                                     inputId = "x",
                                     label = "Displaying Unemployment Rates for 2013-2022",
                                     choices = c("Year"),
                                     selected = "Year"
                                 ), # Radio buttons end
                                 actionButton("run_plot", "Run Plot"),
                                 actionButton("reset", "Clear Output"),
                             ), # side bar panel end
                             mainPanel(
                                 span(strong("Compare State Unemployment Rates Pre and Post COVID.", style = "color:black"),style = "font-si16pt"),
                                 div("Select the state(s) you wish to view from the drop down menu. Once you have made your selections, click \"Run Plot\"."),
                                 br(),
                                 plotlyOutput(outputId = "graph"),
                             ) # Main panel end
                         ) # select input end
                ), #navbar interactive graph
                tabPanel("Data", DT::dataTableOutput(outputId="datasheet"))# navbar data end
    ) #Navbar end
) # fluid page end 

server <- function(input, output, session) {
    
    q_filtered <- eventReactive(input$run_plot, {
        filter(q_long, State %in% input$y)
    })

    output$graph <- renderPlotly({
        ggplot(q_filtered(), aes(x = .data[[input$x]], y = unemployment, color = State)) + geom_point() + geom_line() + geom_vline(aes(xintercept = 2020)) + scale_x_continuous(breaks = q$year)
        
    }) # render plotly end
    output$datasheet<-DT::renderDataTable({
        DT::datatable(data=q,
                      rownames=FALSE)}
    )
} # server end

shinyApp(ui = ui, server = server)

我真的不知道接下来该做什么

可能像这样(未测试):

server <- function(input, output) {
  
  Plot <- reactiveVal()

  q_filtered <- eventReactive(input$run_plot, {
    filter(q_long, State %in% input$y)
  })
  
  observe({
    gg <- ggplot(q_filtered(), aes(x = .data[[input$x]], y = unemployment, color = State)) + geom_point() + geom_line() + geom_vline(aes(xintercept = 2020)) + scale_x_continuous(breaks = q$year)
    Plot(gg)
  })
  
  observeEvent(input$reset, {
    Plot(plotly_empty())
  })
  
  output$graph <- renderPlotly({
    Plot()  
  }) 
  
  output$datasheet <- DT::renderDataTable({
    DT::datatable(data=q,
                  rownames=FALSE)}
  )
} # server end