本文介绍了R闪亮:将绘图重置为默认状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是此处的后续问题。 当我启动闪亮的应用程序时,显示一个图形,然后我想运行一些代码来动画处理数据中的某些采样。 我想实现一个重置/清除按钮来重置绘图到它的原始状态(即,如果我刚刚启动了应用程序)。任何想法? 我当前代码的工作示例: 库(闪亮)库(ggplot2) invalidateLaterNew< - function(millis,session = getDefaultReactiveDomain(),update = TRUE) { if(update) { ctx< - shiny :::。getReactiveEnvironment()$ currentContext() shiny ::: timerCallbacks $ schedule(millis,function(){ if(!is.null( session)&& session $ isClosed()){ return(invisible())} ctx $ invalidate()}) invisible() unlockBinding(invalidateLater,as.environment(package:shiny)) assign(invalidateLater,invalidateLaterNew,package:闪亮) data x = sort(runif(n = 60)),y = sort(runif(n = 60)+ rnorm(60))) ui< - fluidPage( sidebarPanel( sliderInpu t(n,样本数量:,分钟= 10, max = nrow(数据),值= 20), sliderInput(survey,调查次数:, min = 1, max = 10,值= 5), actionButton(button,Go!), actionButton(reset,Reset)),#显示绘图 mainPanel( plotOutput(plot1))) 服务器< - 函数(输入,输出,会话){ plot1< ; - NULL count output $ plot1 plot1 plot1 plot1 }) observeEvent(输入$ button,{ count output $ plot1< - renderPlot({ count<< - count + 1 invalidateLater(500 ,会话,计数<输入$调查)数据$取样< - 红色 sample.rows< - 样本(数据$ ID,输入$ n)数据$取样[sample.rows]< ; - 绿色 plot1 sample.mean.x< - mean(data $ x [sample.rows]) plot1 plot1 })})} #运行应用程序我尝试过包装第一个 renderPlot({...})使用重置按钮输入调用 observeEvent 调用,但没有好处。我也尝试创建第三个 renderPlot({...})调用,它有一个 observeEvent 。 我甚至试图将原始 plot1 复制到第二个变量,并回想起在重置按钮上,但没有运气。解决方案根据我在上一个问题中的评论,我通过添加 plot1<< -NULL< / code>在 observeEvent 中,然后再次渲染原始图。 服务器< - 函数(输入,输出,会话){ plot1< - NULL count output $ plot1 plot1 plot1 plot1 }) observeEvent(输入$按钮,{ plot1 输出$ plot1 plot1 plot1 plot1 }) count count invalidateLater(500,session,count< input $ survey) data $采样< - 红色样本.rows data $ sampled [sample.rows] plot1 sample.mean.x plot1<< - plot1 + geom_vline(xintercept = sample.mean.x,color =green) plot1 } )})} 在上述情况下,您不需要复位按钮。如果你想要一个重置按钮,你可以把绘图<<< -NULL< / code>和< code> renderPlot c $ c> observeEvent 重置按钮。例如: server< - 函数(输入,输出,会话){ plot1 < - NULL count output $ plot1< - renderPlot({ plot1<< - ggplot(data,aes(x = x, y = y))+ geom_point(color =red)+ theme_bw() plot1 plot1 }) observeEvent(输入$ button,{ count 输出$ plot1< ; - renderPlot({ count invalidateLater(500,session,count< input $ survey) data $ sampled< - 红色 sample.rows< - 样品(数据$ ID,输入$ n)数据$取样[sample.rows]< - 绿色 plot1< ;< - plot1 + geom_point(x = data $ x,y = data $ y,color = data $ samples,size = 2) sample.mean.x< - mean x [sample.rows]) plot1<&l t; - plot1 + geom_vline(xintercept = sample.mean.x,color =green) plot1 })}) $ b observeEvent(input $ reset,{ plot1 output $ plot1< - renderPlot({ plot1 plot1 plot1 }) }) } 希望这有助于您! 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 from the data.I would like to implement a reset/clear button to reset the plot to it's original state (i.e. as if I had just started the app again). Any ideas?Working example of my current code:library(shiny)library(ggplot2)invalidateLaterNew <- function (millis, session = getDefaultReactiveDomain(), update = TRUE){ if(update){ ctx <- shiny:::.getReactiveEnvironment()$currentContext() shiny:::timerCallbacks$schedule(millis, function() { if (!is.null(session) && session$isClosed()) { return(invisible()) } ctx$invalidate() }) invisible() }}unlockBinding("invalidateLater", as.environment("package:shiny"))assign("invalidateLater", invalidateLaterNew, "package:shiny")data <- data.frame(ID=1:60, x=sort(runif(n = 60)), y=sort(runif(n = 60)+rnorm(60)))ui <- fluidPage( sidebarPanel( sliderInput("n", "Number of samples:", min = 10, max = nrow(data), value = 20), sliderInput("surveys", "Number of surveys:", min = 1, max = 10, value = 5), actionButton("button", "Go!"), actionButton("reset", "Reset") ), # Show the plot mainPanel( plotOutput("plot1") ))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 }) })}# Run the applicationshinyApp(ui = ui, server = server)I have tried wrapping the first renderPlot({...}) call in an observeEvent call with a reset button input, but no good. I have also tried creating a third renderPlot({...}) call which has an observeEvent.I have even tried copying the "original" plot1 to a second variable and recalling that on the reset button, but no luck. 解决方案 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! 这篇关于R闪亮:将绘图重置为默认状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 21:14