简体   繁体   English

R - Shiny - 如何在观察者中多次更新textOutput

[英]R - Shiny - How to update a textOutput multiple times in an observer

I have a question about updating a text output in a shiny app. 我有一个关于更新闪亮应用程序中的文本输出的问题。

In an observer, I make several computations, and, between each of them, I want to show informations in a text output. 在一个观察者中,我进行了几次计算,并且在每个计算之间,我想在文本输出中显示信息。

I tried several things, but the only thing it is showing is the last information : 我尝试了几件事,但它唯一展示的是最后的信息:

library(shiny)

ui <- fluidPage(
  headerPanel("Hello !"),
  mainPanel(
    actionButton("bouton", "Clic !"),
    textOutput("texte")
  )
)

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

  observeEvent(input$bouton, {
    output$texte = renderText("Initialization...")
    Sys.sleep(1)
    output$texte = renderText("Almost ready...")
    Sys.sleep(3)
    output$texte = renderText("Ok !")
  })
}

runApp(list(ui=ui,server=server), launch.browser = TRUE)

Or : 要么 :

library(shiny)

ui <- fluidPage(
  headerPanel("Hello !"),
  mainPanel(
    actionButton("bouton", "Clic !"),
    textOutput("texte")
  )
)

server <- function(input,output, session){
  rv = reactiveValues()

  rv$mess = ""

  observeEvent(input$bouton, {
    rv$mess = "Initialization..."
    Sys.sleep(1)
    rv$mess = "Almost ready..."
    Sys.sleep(3)
    rv$mess = "Ok !"
  })
  observe({
    output$texte <<- renderText(rv$mess)
  })
}

runApp(list(ui=ui,server=server))

Edit : in these two examples, it shows nothing until the last message "OK !" 编辑:在这两个例子中,它只显示最后一条消息“OK!”

Where am I wrong ? 我哪里错了?

Thanks for your help ! 谢谢你的帮助 !

Thanks to Eugene, this is my working piece of code (server only) : 感谢Eugene,这是我的工作代码(仅限服务器):

server <- function(input,output, session){
  rv = reactiveValues()

  rv$mess = ""

  observeEvent(input$bouton, {
    withProgress({
      setProgress(message = "Initialization...")
      Sys.sleep(1)
      setProgress(message = "Almost ready...")
      Sys.sleep(3)
      setProgress(message = "Ok !")
      Sys.sleep(2)
    })
  })
}

You might consider achieving this with shiny's progress indicators by: 您可以考虑使用闪亮的进度指标实现此目标:

  1. wrapping everything in your observer in withProgress , and 将所有内容包装在withProgress的观察者中,和
  2. using setProgress( message = "some message" ) where you use rv$mess and output$texte 使用setProgress( message = "some message" ) ,你使用rv$messoutput$texte

However, the progress indicator will show up in the top-right (or elsewhere if you modify the css) and not in your output box. 但是,进度指示器将显示在右上角(如果您修改了css,则显示在其他位置)而不是输出框中。

http://shiny.rstudio.com/articles/progress.html http://shiny.rstudio.com/articles/progress.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM