[英]How to render more than once when using observeEvent in shiny?
我使用shiny
的R和想用的時候渲染不止一次observeEvent
在server.R
我不明白為什么這行不通:
library(shiny)
my_UI <- fluidPage(
fluidRow(actionButton("test", "test")),
fluidRow(textOutput("out"))
)
my_Server <- function(input, output) {
observeEvent(input$test, {
output$out <- renderText("waiting")
Sys.sleep(2)
output$out <- renderText("done")
})
}
shinyApp(my_UI, my_Server)
我也嘗試過:
library(shiny)
my_UI <- fluidPage(
fluidRow(actionButton("test", "test")),
fluidRow(textOutput("out"))
)
my_Server <- function(input, output) {
observeEvent(input$test, {
output$out <- renderText("waiting")
})
observeEvent(input$test, {
Sys.sleep(2)
output$out <- renderText("done")
})
}
shinyApp(my_UI, my_Server)
在這兩種情況下,僅呈現后一個消息……我在這里做錯了什么? 謝謝!
我無法在一個observeEvent中為每一行渲染提供解決方案,但是如果您只想顯示加載/計算進度,則可以使用
withProgress()
所以應用程序可以像這樣:
library(shiny)
my_UI <- fluidPage(
fluidRow(actionButton("test", "test")),
fluidRow(textOutput("out"))
)
my_Server <- function(input, output) {
observeEvent(input$test, {
withProgress(
message = 'Calculation in progress',
detail = 'This may take a while...', value = 0, {
incProgress(1/2, message = "Step 1")
Sys.sleep(2)
# Do some stuff, load, calculate, etc
incProgress(1/2, message = "Step 2")
})
})
}
shinyApp(my_UI, my_Server)
您無法使用Shiny進行此操作,因為將僅使用最新的輸出值。
你可以用JavaScript做,但你可能有更好的選擇,即withProgress
為feniw_fx mentionned
library(shiny)
my_UI <- fluidPage(
tags$head(
tags$script(
"function test() {
document.getElementById('out').innerHTML = 'waiting';
setTimeout(function() {document.getElementById('out').innerHTML = 'done'}, 2000);}"
)),
fluidRow(actionButton("test", "test", onclick = "test()")),
fluidRow(textOutput("out"))
)
my_Server <- function(input, output, session) {
}
shinyApp(my_UI, my_Server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.