R闪亮:显示特定时间段的文本输出



在我的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)

以下是一个具有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)

最新更新