当计时器倒计时到零时,让闪亮的应用程序做出反应

时间:2017-03-23 04:55:46

标签: r shiny e

我已将计时器设置为无功值。值$总。它被设计为2017-03-22 23:55:40倒计时,当值$ total计算到零时,它应该添加到value2 $ total的计数。但是,当我启动应用程序时,值$ total被冻结并且没有移动。我该如何让它倒计时?

library(shiny)
library(lubridate)

ui <- fluidPage(
  "Total:",
  textOutput("total", inline = TRUE),
  "Draft Count:",
  textOutput("draft_count", inline = TRUE),
  "Time:",
  textOutput("time",inline=TRUE)
)

server <- function(input, output, session) {
  values <- reactiveValues(total = as.numeric(as.POSIXct("2017-03-22 23:55:40")-now(),units="secs"))
  values2 <- reactiveValues(total = 0)

  observeEvent(values$total <= 0,{
    values2$total <- values2$total + 1
  })

  output$total <- renderText({
    invalidateLater(1000,session)
    values$total
  })

  output$draft_count <- renderText({
    invalidateLater(1000,session)
    values2$total
  })

  output$time <- renderText({
    invalidateLater(1000,session)
    as.character(as.POSIXct(now()))
  })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我想这就是你想要的:

server <- function(input, output, session) {
#values <- reactiveValues(total = as.numeric(as.POSIXct("2017-03-22 23:55:40")-now(),units="secs"))
  val <- reactive({
    invalidateLater(1000,session)
    <- as.numeric(as.POSIXct("2017-03-22 23:55:40")-now(),units="secs")
  } )
  values2 <- reactiveValues(total = 0)

  observeEvent(values$total <= 0,{
    values2$total <- values2$total + 1
  })

  output$total <- renderText({
    invalidateLater(1000,session)
    val()
  })

  output$draft_count <- renderText({
    invalidateLater(1000,session)
    values2$total
  })

  output$time <- renderText({
    invalidateLater(1000,session)
    as.character(as.POSIXct(now()))
  })
}