R闪亮的应用程序中是否存在可见的秒表(可启动和可停止),其秒数可以用作输入值?
我不知道任何实现。有没有简单的方法可以做到这一点? 感谢您的提前答复
答案 0 :(得分:3)
这是一种可能的解决方案,根据我的答案here改编为倒数计时器。
希望这会有所帮助!
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)