简体   繁体   中英

R: Button to reset a plot using shiny apps

How can I reset the graph to display a blank plot? I've created a reset button and have tried various recommendations, but they usually cause some sort of problem or they do nothing at all.

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)

I am just really not sure what to do from here

Maybe like this (not tested):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM