【问题标题】:Call updateTabItems from inside Shiny module从 Shiny 模块中调用 updateTabItems
【发布时间】:2018-12-04 22:46:28
【问题描述】:

在我的闪亮应用程序中,我有很多 valueBoxes,每个代表闪亮仪表板侧边栏中的 tabItem。单击 valueBox 时,页面应移动到正确的选项卡。

我没有多次复制粘贴代码,而是编写了一个可重用模块,该模块呈现 valueBox 并将 valueBox 的类更改为 actionButton。在服务器部分中,我包含了一个 observeEvent,它在单击 valueBox 时调用 updateTabItems。但是当点击什么都没有发生。该模块似乎无法操作仪表板侧边栏。

library(shiny)
library(shinydashboard)

value_box_output <- function(.id) {
  ns <- NS(.id)
  valueBoxOutput(ns("overview.box"))
}

value_box <- function(input, output, session, .value, .subtitle, .tab.name) {
  ns <- session$ns

  output$overview.box <- renderValueBox({
    box1 <- valueBox(
      .value,
      .subtitle,
      href = "#",
      width = NULL
    )
    box1$children[[1]]$attribs$class <- "action-button"
    box1$children[[1]]$attribs$id <- ns("button")
    box1
  })

  observeEvent(input$button, {
    print("clicked")
    updateTabItems(session, inputId = "tabs", selected = .tab.name)
  })
}


ui <- dashboardPage(
  dashboardHeader(title = "Title"),
  dashboardSidebar(
    sidebarMenu(
    id = "tabs",
    menuItem("Overview", tabName = "Overview"),
    menuItem("Tab 1", tabName = "Tab_1")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("Overview", value_box_output("tab")),
      tabItem("Tab_1")
    )
  )
)

server <- function(input, output, session) {
  callModule(value_box,
             "tab",
             .value = 33,
             .subtitle = "Tab 1",
             .tab.name = "Tab_1")
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您可以在这篇文章中找到答案:Accessing parent namespace inside a shiny module

    基本上,在模具内的 updateTabItems() 中,您需要调用父级的会话,而不是模块的会话。

    因此,为您的会话添加一个变量到 callModule() 并在 updateTabItems() 中调用它。

    【讨论】:

      猜你喜欢
      • 2017-02-16
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 2018-02-13
      • 2016-08-10
      • 2018-08-09
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多