【问题标题】:Displaying real time in a text input box在文本输入框中实时显示
【发布时间】:2019-01-16 21:06:08
【问题描述】:

我有一个使用 Sys.time() 作为其值的文本输入框,但是随着正常时间的流逝,它保持静态,我一直在尝试刷新时间以显示新值,但目前我唯一的方法'已经能够通过重新启动应用程序来更新时间。

我尝试过使用 textoutput() 和 invalidate later() 函数,但是这只显示可以说“滴答”的当前时间,但仍然需要用户输入,这是多余的,因为重点是让它们不输入时间。此输入和其他一些输入最终通过 googlesheets 包上传到 google sheet。

这是我当前的代码:

Stime <- as.character(format(Sys.time(), "%H:%M", "EST"))

textInput(
          inputId = "Time",
          label = "Today's Time",
          value = Stime)

what it looks like now (via imgur) except it is static

我唯一想做的就是增加时间!任何帮助表示赞赏!

【问题讨论】:

    标签: r date time shiny


    【解决方案1】:

    来自 R Shiny 网站:

    http://shiny.rstudio.com/gallery/current-time.html

    options(digits.secs = 3) # Include milliseconds in time display
    
    function(input, output, session) {
    
      output$currentTime <- renderText({
        # invalidateLater causes this output to automatically
        # become invalidated when input$interval milliseconds
        # have elapsed
        invalidateLater(as.integer(input$interval), session)
    
        format(Sys.time())
      })
    }
    

    【讨论】:

    • 我之前实现了,但我只是再次尝试,现在我收到错误:Error in thisFunc() : could not find function "thisFunc" 现在即使注释掉了应用程序也无法运行,我'我搜索了错误,我现在唯一能找到的就是重新启动会话。
    • 如果没有导致错误的代码,我无法告诉您发生了什么。鉴于您的函数名称,您似乎已尝试更改某些内容。如果您复制确切的代码,它是否有效?
    • 我完全复制了它,只更改了输入 ID,我仍然收到前面提到的错误,我目前已将其注释掉,但启用它会导致应用程序“崩溃”可以这么说.但是,我想澄清一下,虽然它表示实时,但它并没有在实际输入框中显示为我想要的值。到目前为止,我在框中列出了时间,但它并没有像我希望的那样增加。
    【解决方案2】:

    我已根据要求对shiny 示例从http://shiny.rstudio.com/gallery/current-time.html 稍作修改,以帮助在textInput 框中显示时间:

    library(shiny)
    ui <- shiny::fluidPage(shiny::uiOutput("ui"))
    
    server <- function(input, output, session) {  
      output$ui <- shiny::renderUI({
        invalidateLater(1000, session)
        shiny::textInput(
          inputId = "time",
          label = "Today's Time",
          value = as.character(format(Sys.time(), "%H:%M", "EST"))
        )
      })
    }
    
    shiny::shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 对于超级迟到的回复,我深表歉意,我已经调整了我写给你的例子的内容,它只更新页面或应用程序手动刷新。我现在还在努力。
    • @SebastianGoslin 在我这边,当我测试它时,它不需要刷新即可更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2013-12-05
    相关资源
    最近更新 更多