【问题标题】:R Shiny: show text output for a certain time periodR Shiny:显示特定时间段的文本输出
【发布时间】:2020-02-29 16:56:23
【问题描述】:

在我的 Shiny 应用程序中,我希望在单击操作按钮后在 UI 中显示一些文本,然后它应该会在几秒钟后再次消失。我怎样才能做到这一点?我尝试使用 InvalidateLater、Sys.sleep() 和 ConditionalPanel,但没有成功。 下面是一个简单的例子。它显示点击后的时间。我希望时间在几秒钟后消失。如果再次单击,则必须出现一个新时间。 我该怎么做?

ui <- fluidPage(
  actionButton("btn", "Click to show current time"),
  textOutput("temp_text")
)

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

  # make text with time stamp after click  
  observeEvent(input$btn, {
    output$temp_text <- renderText({
      paste0("The time is: ", strftime(Sys.time(), format = "%H:%M:%S"))
    })

  })

}

shinyApp(ui, server)

【问题讨论】:

  • 可以在警报窗口中显示文本吗?否则,您希望如何显示文本?您的期望不够明确。
  • 不,我想将文本显示为 TextOutput。我添加了一个示例。

标签: r shiny


【解决方案1】:

这里有shinyjsdelay函数的解决方案:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("btn", "Click to show current time"),
  textOutput("temp_text")
)

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

  Time <- reactiveVal()
  observeEvent(input$btn, {
    Time(paste0("The time is: ", strftime(Sys.time(), format = "%H:%M:%S")))
  })

  output$temp_text <- renderText({
    Time()
  })

  observeEvent(input$btn, {
    delay(ms = 2000, Time(NULL))
  })

}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2014-12-06
    • 1970-01-01
    • 2021-09-08
    • 2018-09-30
    • 2018-02-27
    • 1970-01-01
    • 2019-02-07
    相关资源
    最近更新 更多