在Rshiny 中,我想创建一个动画,它由一个动作按钮启动,并可以被另一个动作按钮中断.以下是不起作用的代码的虚拟版本.目标是开始一次迭代,每秒打印一次Year=Year+1,直到它达到最大值.然而,用户也应该能够使用停止按钮来中断该过程.

例如,我之所以使用观察事件()、观察()和reactive veValues()构造而不是for循环,是因为我无法停止由Start按钮启动的循环.

在本例中,启动或停止函数都不起作用.使用for循环,我让迭代开始,但Stop按钮仅在迭代结束时"停止"该过程.在for循环中包含observeEvent(input$stop, { break })的构造不起作用.我猜原因是,只有在执行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) })

    })

推荐答案

这个比例有点偏离了你的nonfunctioning example code,不过,我想知道你是否知道sliderInput的动画功能.

我认为这可能会省go 您编写大量定制代码:

library(shiny)

ui <- fluidPage(
  tags$head(tags$style(
    HTML(".slider-animate-button {
            opacity: 1;
          }
         ")
  )),
  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; color: #fff; background-color: #337ab7; border-color: #2e6da4"
          ),
          pauseButton = actionButton(
            "pause",
            "Pause",
            icon = icon("pause"),
            width = "100px",
            style = "margin-top: 10px; color: #fff; background-color: #337ab7; border-color: #2e6da4"
          )
        ),
        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)

result

R相关问答推荐

从具有随机模式的字符串中提取值

如何将具有重复名称的收件箱合并到R中的另一列中,而结果不同?

计算R中的威布尔分布的EDF

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

根据选中三个复选框中的一个或两个来调整绘图

如何得到R中唯一的组合群?

计算具有奇数日期的运行金额

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

使用R闪光显示所有数据点作为默认设置

如何在分组条形图中移动相关列?

如何在R forestplot中为多条垂直线分配唯一的 colored颜色 ?

R Select()可以测试不存在的子集列

Data.table';S GForce-将多个函数应用于多列(带可选参数)

在R中创建连续的期间

将全局环境变量的名称分配给列表中的所有元素

R中Gamma回归模型均方误差的两种计算方法不一致

将摘要图添加到facet_WRAP gglot的末尾

带RStatix的Wilcoxon环内检验

R基于变量组合创建新的指标列

如何使用循环从R中的聚合函数创建列,而不会在名称中给出&q;$&q;?