Search code examples
rshinyshinydashboardshiny-server

How to return back to the first plot in R-Shiny?


I am working on an R-Shiny Application. I have used the following code(demo code) to interact with the plot.

  ui <- shinyUI(fluidPage(
   titlePanel("Title"),
   sidebarLayout(
     sidebarPanel(
     ),
     mainPanel(
       plotOutput("graph", width = "100%", click = "plot_click"),
       verbatimTextOutput("click_info")
     )
   )
 ) 
 )

 server <- shinyServer(function(input, output, session) { 
   observe({
     output$graph <- renderPlot({
       plot(1, 1)
     })  
   })


   # interaction click in graph  
   observe({
     if(is.null(input$plot_click$x)) return(NULL)
     x <- sample(20:30,1,F)
     isolate({
       output$graph <- renderPlot({
         draw.single.venn(x)
       }) 
     })

   })
 })
 shinyApp(ui=ui,server=server)

It can change the plot on a mouse click. I want to get back to the very first plot using a reset button. Kindly help.


Solution

  • I added a reset button to your sidebar. Hope that's helpful. This link provides more info on how to do this type of functionality.

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      titlePanel("Title"),
      sidebarLayout(
        sidebarPanel(
          actionButton("Reset", label="Reset Graph")
        ),
        mainPanel(
          plotOutput("graph", width = "100%", click = "plot_click"),
          verbatimTextOutput("click_info")
        )
      )
    ) 
    )
    
    server <- shinyServer(function(input, output, session) { 
      observeEvent(input$Reset,{ output$graph <- renderPlot({ plot(1, 1) }) }, ignoreNULL = F)
    
      # interaction click in graph  
      observe({
        if(is.null(input$plot_click$x)) return(NULL)
        x <- sample(20:30,1,F)
        isolate({
          output$graph <- renderPlot({
            draw.single.venn(x)
          }) 
        })
    
      })
    
    })
    shinyApp(ui=ui,server=server)