R Shiny等待会话空闲后再执行下一条指令

zlwx9yxi  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(135)

我有一个很棒的应用程序,我想自动访问每个选项卡,并等待选项卡渲染完成后再移动到下一个选项卡。(背景是,它会进行一些计算并构建缓存。但严格来说,我猜这不是问题的一部分。)
我有一个简单的应用程序,有三个选项卡(当访问时,将消息打印到屏幕和控制台)。该应用程序有一个React值current_tab(),允许我自动转到一个选项卡。
最后,我循环遍历所有选项卡以呈现选项卡。

library(shiny)

ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      id = "tab_panel",
      tabPanel("tab1", h1("This is Tab 1"), textOutput("txt1")),
      tabPanel("tab2", h1("This is Tab 2"), textOutput("txt2")),
      tabPanel("tab3", h1("This is Tab 3"), textOutput("txt3"))
    )
  )
)

server <- function(input, output, session) {
  # 1. Render some text to the textOutput ----
  # use print to also print the text to the console
  output$txt1 <- renderText(print("Rendering Text 1"))
  output$txt2 <- renderText(print("Rendering Text 2"))
  output$txt3 <- renderText(print("Rendering Text 3"))
  
  # 2. Create a reactiveVal that allows me to go to a tab ----
  current_tab <- reactiveVal()
  observe(cat(sprintf("Currently on tab %s\n", current_tab())))
  # move to the tab
  observeEvent(current_tab(), {
    updateTabsetPanel(session, "tab_panel", selected = current_tab())
  })
  
  # 3. Iterate through all tabs and render them ----
  tabs <- c("tab1", "tab2", "tab3")
  for (tab in tabs) {
    print(paste("Trying to move to Tab", tab))
    current_tab(tab)
    # Wait until the session is free again before moving to the next one?!
  }
}

shinyApp(ui, server)

不幸的是(这是问题的主要部分),for循环快速迭代选项卡,因此选项卡没有呈现(在输出中,我们从选项卡1开始,它被呈现,然后for循环跳到2(跳过呈现部分),然后跳到3,它被呈现,因为循环已经完成,会话有时间呈现它)。
最后,我需要所有三个选项卡在控制台中显示的文本Rendering Text X
有什么想法吗?

1l5u6lss

1l5u6lss1#

编辑:旧答案在底部,新答案在顶部
你可以这样做:

library(shiny)

ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      id = "tab_panel",
      tabPanel("tab1", h1("This is Tab 1"), textOutput("txt1")),
      tabPanel("tab2", h1("This is Tab 2"), textOutput("txt2")),
      tabPanel("tab3", h1("This is Tab 3"), textOutput("txt3"))
    )
  )
)

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

  content1<-reactive(
    print("Rendering Text 1")
    )
  content2<-reactive(
    print("Rendering Text 2")
  )
  content3<-reactive(
    print("Rendering Text 3")
  )
  output$txt1 <- renderText({
    content1()
    })
  output$txt2 <- renderText({
    content2()})
  output$txt3 <- renderText({
    content3()
})
  
  observe(
    list(content1(),
         content2(),
         content3())
  )
}
shinyApp(ui, server)

如果你想禁用光泽的某些方面的懒惰(谨慎使用),你可以这样做。2这会达到你想要的效果。

library(shiny)

ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      id = "tab_panel",
      tabPanel("tab1", h1("This is Tab 1"), textOutput("txt1")),
      tabPanel("tab2", h1("This is Tab 2"), textOutput("txt2")),
      tabPanel("tab3", h1("This is Tab 3"), textOutput("txt3"))
    )
  )
)

server <- function(input, output, session) {
  # 1. Render some text to the textOutput ----
  # use print to also print the text to the console
  output$txt1 <- renderText(print("Rendering Text 1"))
  output$txt2 <- renderText(print("Rendering Text 2"))
  output$txt3 <- renderText(print("Rendering Text 3"))
  outputOptions(output, "txt2", suspendWhenHidden = FALSE)
  outputOptions(output, "txt3", suspendWhenHidden = FALSE)
}

shinyApp(ui, server)

相关问题