秒表作为 shiny for R 的输入

Stopwatch as input in shiny for R

是否有可能在 R shiny 应用程序中有一个可见的秒表(可启动和可停止),其中的秒数可以用作输入值?

我不知道任何实现。有没有简单的方法可以做到这一点? 提前感谢您的回答

这是一种可能的解决方案,改编自我对倒数计时器的回答

希望对您有所帮助!



library(lubridate)
library(shiny)

ui <- fluidPage(
  hr(),
  actionButton('start','Start'),
  actionButton('stop','Stop'),
  actionButton('reset','Reset'),
  tags$hr(),
  textOutput('timeleft')

)

server <- function(input, output, session) {

  # Initialize the timer, not active.
  timer <- reactiveVal(0)
  active <- reactiveVal(FALSE)
  update_interval = 0.1 # How many seconds between timer updates?

  # Output the time left.
  output$timeleft <- renderText({
    paste("Time passed: ", seconds_to_period(timer()))
  })

  # observer that invalidates every second. If timer is active, decrease by one.
  observe({
    invalidateLater(100, session)
    isolate({
      if(active())
      {
        timer(round(timer()+update_interval,2))
      }
    })
  })

  # observers for actionbuttons
  observeEvent(input$start, {active(TRUE)})
  observeEvent(input$stop, {active(FALSE)})
  observeEvent(input$reset, {timer(0)})

}

shinyApp(ui, server)