从循环内发布更新渲染文本
Issue updating render text from within a loop
我正在尝试弄清楚如何通过循环为每次迭代更新渲染文本。我正在尝试创建一个状态栏,在其中为循环中的每个循环输出一些文本。目前文本仅在循环完成后显示,显示列表中的最后一个值。 updateprogressbar 工作得很好。
有什么建议吗?
可以在下面找到最小的可重现性。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h4(textOutput("loadingtxt")),
progressBar(id = "pbmod", value = 0, display_pct = TRUE, striped = TRUE)
)
server <- function(input, output, session) {
observeEvent(session, {
messages <- data.frame(c("Loading Environment...", "Parsing data...", "Preparing Live Data Feeds...", "Pondering what to display next...", "Calculating Llama Expectoration Trajectory...", "Breeding Fauna...", "Charging Ozone Layer...", "Compressing Fish Files...", "Speculating Stock Market Indices...", "Application Ready!"), stringsAsFactors = FALSE)
for (i in 1:10) {
Sys.sleep(0.7)
status <- messages[i,]
status
output$loadingtxt <- renderText(status)
updateProgressBar(session = session, id = "pbmod", value = 100/10*i)
}
})
}
shinyApp(ui, server)
这里有一个选项。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h4(textOutput("loadingtxt")),
progressBar(id = "pbmod", value = 0, display_pct = TRUE, striped = TRUE)
)
server <- function(input, output, session) {
messages <- data.frame(c("Loading Environment...", "Parsing data...", "Preparing Live Data Feeds...", "Pondering what to display next...", "Calculating Llama Expectoration Trajectory...", "Breeding Fauna...", "Charging Ozone Layer...", "Compressing Fish Files...", "Speculating Stock Market Indices...", "Application Ready!"), stringsAsFactors = FALSE)
status <- reactiveVal()
step <- reactiveVal()
autoInvalidate <- reactiveTimer(700)
i <- 0
observe({
autoInvalidate()
i <<- i+1
if(i == 9){ # stop the reactive timer
autoInvalidate <<- reactiveTimer(Inf)
}
step(i)
status(messages[i,])
})
output$loadingtxt <- renderText({
updateProgressBar(session = session, id = "pbmod", value = 100/10*step())
status()
})
}
shinyApp(ui, server)
我正在尝试弄清楚如何通过循环为每次迭代更新渲染文本。我正在尝试创建一个状态栏,在其中为循环中的每个循环输出一些文本。目前文本仅在循环完成后显示,显示列表中的最后一个值。 updateprogressbar 工作得很好。
有什么建议吗?
可以在下面找到最小的可重现性。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h4(textOutput("loadingtxt")),
progressBar(id = "pbmod", value = 0, display_pct = TRUE, striped = TRUE)
)
server <- function(input, output, session) {
observeEvent(session, {
messages <- data.frame(c("Loading Environment...", "Parsing data...", "Preparing Live Data Feeds...", "Pondering what to display next...", "Calculating Llama Expectoration Trajectory...", "Breeding Fauna...", "Charging Ozone Layer...", "Compressing Fish Files...", "Speculating Stock Market Indices...", "Application Ready!"), stringsAsFactors = FALSE)
for (i in 1:10) {
Sys.sleep(0.7)
status <- messages[i,]
status
output$loadingtxt <- renderText(status)
updateProgressBar(session = session, id = "pbmod", value = 100/10*i)
}
})
}
shinyApp(ui, server)
这里有一个选项。
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
tags$h4(textOutput("loadingtxt")),
progressBar(id = "pbmod", value = 0, display_pct = TRUE, striped = TRUE)
)
server <- function(input, output, session) {
messages <- data.frame(c("Loading Environment...", "Parsing data...", "Preparing Live Data Feeds...", "Pondering what to display next...", "Calculating Llama Expectoration Trajectory...", "Breeding Fauna...", "Charging Ozone Layer...", "Compressing Fish Files...", "Speculating Stock Market Indices...", "Application Ready!"), stringsAsFactors = FALSE)
status <- reactiveVal()
step <- reactiveVal()
autoInvalidate <- reactiveTimer(700)
i <- 0
observe({
autoInvalidate()
i <<- i+1
if(i == 9){ # stop the reactive timer
autoInvalidate <<- reactiveTimer(Inf)
}
step(i)
status(messages[i,])
})
output$loadingtxt <- renderText({
updateProgressBar(session = session, id = "pbmod", value = 100/10*step())
status()
})
}
shinyApp(ui, server)