r - 定期切换 Shiny 仪表板中的选项卡

标签 r shiny

在带有多个选项卡的 shinydashboard 中,如下所示

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Sample Shiny"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Tab 1", tabName = "tab1"),
      menuItem("Tab 2", tabName = "tab2")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        "tab1",
        fluidRow(
          box(title = "Foo")
        )
      ),
      tabItem(
        "tab2",
        fluidRow(
          box(title = "Bar")
        )
      )
    )
  )
)

server <- function(input, output) { }

shinyApp(ui = ui, server = server)

是否可以让应用程序定期切换事件选项卡?我希望屏幕上显示仪表板,并且每 x 分钟切换一次事件选项卡。

我已经检查了 Shiny 文档以获取解决方案,但尚未找到合适的函数。但也许我只是忽略了这样一个功能。如果 Shiny 没有提供合适的功能,是否可以包含一些自定义 JavaScript 来完成这项工作?

最佳答案

这是一种使用 invalidateLater 和 updateTabItems 的方法:

应用程序.R:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Sample Shiny"),
  dashboardSidebar(
    sidebarMenu(
      id = 'tabs',
      menuItem("Tab 1", tabName = "tab1"),
      menuItem("Tab 2", tabName = "tab2")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        "tab1",
        fluidRow(
          box(title = "Foo")
        )
      ),
      tabItem(
        "tab2",
        fluidRow(
          box(title = "Bar")
        )
      )
    )
  )
)

tabnames = c('tab1', 'tab2')
server <- function(input, output, session) {
  #keep track of active tab
  active <- reactiveValues(tab = 1)

  observe({
    #Change every 5 secs, you can set this to whatever you want
    invalidateLater(5000,session)
    #update tab
    isolate(active$tab <- active$tab%%length(tabnames) + 1)
    updateTabItems(session,'tabs',tabnames[active$tab])
  })
}

shinyApp(ui = ui, server = server)

关于r - 定期切换 Shiny 仪表板中的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46504547/

相关文章:

r - 在 R : legend and lines 中用线密度代替颜色绘制 map

r - 我如何在 Spark shell 中加载持久数据帧

R - 下载过滤后的数据表

r - 如何为 DT 表的给定行间隔赋予颜色?

r - 使用验证 Shiny 检查 reactiveValue 是否存在 - 未找到

r - mapdeck 中的 add_polygon 缩小 map

r - 为什么 reprex 无法渲染 %>% 结果

r - 数字输入到 Shiny 中的数据框

javascript - R 中的 MVC 模式

r - 我 Shiny 的应用程序的登录屏幕没有超时