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

R:显示特定时间段的文本输出,r,shiny,R,Shiny,在我闪亮的应用程序中,我想在点击一个动作按钮后在UI中显示一些文本,然后在几秒钟后它会再次消失。我该怎么做?我尝试使用InvalidateLater、Sys.sleep()和ConditionalPanel,但没有成功。 下面是一个简单的例子。它显示单击后的时间。我希望时间在几秒钟后消失。如果再次单击,则必须显示新时间。 我该怎么做 ui <- fluidPage( actionButton("btn", "Click to show current time"), textOut

在我闪亮的应用程序中,我想在点击一个动作按钮后在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)

ui这是一个解决方案,它具有
shinyjs
delay
功能:

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)
库(闪亮)
图书馆(shinyjs)

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