R shiny: reset plot to default state

后端 未结 1 962
迷失自我
迷失自我 2020-12-22 08:28

This is a follow up to my question here.

I have a plot that is displayed when I start the shiny app, then I want to run some code which \"animates\" some sampling fr

相关标签:
1条回答
  • 2020-12-22 08:54

    As per my comment in your previous question I have done the changes by adding plot1<<-NULL inside the observeEvent and then again render the original plot.

    server <- function(input, output, session) {
    
        plot1 <- NULL
        count <- 0
    
        output$plot1 <- renderPlot({
          plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
          plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
          plot1
        })
    
        observeEvent(input$button,{
          plot1 <<- NULL
    
          output$plot1 <- renderPlot({
            plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
            plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
            plot1
          })
    
          count <<- 0
          output$plot1 <- renderPlot({
    
            count <<- count+1
            invalidateLater(500, session,  count < input$surveys)
            data$sampled <- "red"
            sample.rows <- sample(data$ID, input$n)
            data$sampled[sample.rows] <- "green"
    
            plot1 <<- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)
    
            sample.mean.x <- mean(data$x[sample.rows])
    
            plot1 <<- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")
    
            plot1
    
          })
        })
      }
    

    In the above case you do not need the reset button. In case if you want a reset button you can put the plot<<-NULL and renderPlot inside the observeEvent of the reset button. Something like this:

     server <- function(input, output, session) {
    
        plot1 <- NULL
        count <- 0
    
        output$plot1 <- renderPlot({
          plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
          plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
          plot1
        })
    
        observeEvent(input$button,{
    
          count <<- 0
          output$plot1 <- renderPlot({
    
            count <<- count+1
            invalidateLater(500, session,  count < input$surveys)
            data$sampled <- "red"
            sample.rows <- sample(data$ID, input$n)
            data$sampled[sample.rows] <- "green"
    
            plot1 <<- plot1 + geom_point(x=data$x, y=data$y, colour=data$sampled, size=2)
    
            sample.mean.x <- mean(data$x[sample.rows])
    
            plot1 <<- plot1 + geom_vline(xintercept = sample.mean.x, colour="green")
    
            plot1
    
          })
        })
    
    
        observeEvent(input$reset,{
    
          plot1<<- NULL
    
    
          output$plot1 <- renderPlot({
            plot1 <<- ggplot(data, aes(x=x, y=y)) + geom_point(colour="red") + theme_bw()
            plot1 <<- plot1 + geom_vline(xintercept = mean(data$x), size=1.1, colour="red")
            plot1
          })
    
    
        })
    
      }
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题