[英]R Shiny: How to start and stop an iteration / animation using action buttons
In R Shiny, I would like to create an animation that is started by an action button and can be interrupted by another action button.在 R Shiny 中,我想创建一个 animation 由一个动作按钮启动,并且可以被另一个动作按钮中断。 Below is a dummy version of the nonfunctioning code.以下是无效代码的虚拟版本。 The goal is to start an iteration that prints year=year+1 every second until it reaches the max.目标是开始迭代,每秒打印 year=year+1 直到达到最大值。 The user, however, should also be able to interrupt the process with the stop button.然而,用户也应该能够使用停止按钮中断该过程。
The reason why I am using the observeEvent(), observe(), and reactiveValues() construction rather than a for loop, for instance, is that I was not able to stop the loop started by the start button.例如,我使用observeEvent()、observe() 和reactiveValues() 构造而不是for 循环的原因是我无法停止由开始按钮启动的循环。
In this example, neither start nor stop function work.在这个例子中,既不启动也不停止 function 工作。 Using a for loop, I get the iteration to start but the stop button only "stops" the process at the end of the iteration.使用 for 循环,我开始迭代,但停止按钮仅在迭代结束时“停止”该过程。 Constructions with observeEvent(input$stop, { break })
within a for loop do not work.在 for 循环中使用observeEvent(input$stop, { break })
的构造不起作用。 The reason is, I guess, that the event is only observed after execution of the for loop as the previous process is still active until then.我猜原因是,该事件仅在执行 for 循环后才被观察到,因为在此之前之前的进程仍然处于活动状态。
shinyApp(
ui=fluidPage(
actionButton("start", "Start"),
actionButton("stop", "Stop"),
actionButton("reset", "Reset"),
verbatimTextOutput("text")
),
server=function(input, output, session) {
r <- reactiveValues(animation = FALSE, year=2000)
observeEvent(input$start, {
r$animation <- TRUE
})
observeEvent(input$stop, {
r$animation <- FALSE
})
observeEvent(input$reset, {
r$animation <- FALSE
r$year <- 2000
})
observe({
if(isTRUE(r$animation)) {
r$year <- r$year + 1
Sys.sleep(1)
if(r$year==2005) {
r$animation <- FALSE
r$year <- 2000
}
}
})
output$text <- renderText({ paste0("It is year ", r$year) })
})
This apporach is a little drifted away from your nonfunctioning example code however, I was wondering whether you are aware of sliderInput
's animation capabilities.这个方法与您的非功能示例代码有点偏离,但是我想知道您是否知道sliderInput
的 animation 功能。
I thought this might save you writing a lot of custom code:我认为这可能会节省您编写大量自定义代码:
library(shiny)
ui <- fluidPage(
titlePanel("Animation Slider Test"),
sidebarLayout(
sidebarPanel(
sliderInput(
inputId = "animation_slider",
label = "Animation Slider",
min = 2000L,
max = 2020L,
value = 2000L,
step = 1L,
round = FALSE,
ticks = TRUE,
animate = animationOptions(
interval = 1000,
loop = FALSE,
playButton = actionButton("play", "Play", icon = icon("play"), width = "100px", style = "margin-top: 10px"),
pauseButton = actionButton("pause", "Pause", icon = icon("pause"), width = "100px", style = "margin-top: 10px")
),
width = NULL,
sep = "",
pre = "Year: ",
post = NULL,
timeFormat = NULL,
timezone = NULL,
dragRange = TRUE
),
actionButton("reset", "Reset", icon = icon("rotate-left"), width = "100px", style = "margin-top: -87px")
),
mainPanel(
verbatimTextOutput("text")
)
)
)
server <- function(input, output, session) {
observeEvent(input$reset, {
updateSliderInput(
session = session,
inputId = "animation_slider",
label = NULL,
value = 2000L,
min = NULL,
max = NULL,
step = NULL,
timeFormat = NULL,
timezone = NULL
)
})
output$text <- renderText({ paste("It is year", input$animation_slider) })
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.