【问题标题】:How to switch between shiny tab panels from inside a module?如何从模块内部在闪亮的标签面板之间切换?
【发布时间】:2020-09-19 22:15:54
【问题描述】:

我正在尝试模块化一个闪亮的应用程序,该应用程序有一个按钮,可以使用 updateTabsetPanel() 在 tabPanel 之间进行反应性切换。这在非模块化应用程序中有效,但在将反应式包含在模块中时无效。我认为这与模块范围之外的面板有关。以下是最小的示例(有效的非模块化版本和无效的模块化版本)。有没有办法让模块与 tabPanel 对话?

# Non-modular version
library(shiny)
library(shinydashboard)

ui <- fluidPage(
  fluidRow(
    column(
      width = 12,
      tabBox(
        id = "tabset2",
        type = "tabs",
        selected = "1",
        tabPanel("T1", value = "1", 
                 fluidRow(
                   box(width = 9, "Some content here"),
                   box(width = 3, 
                       actionButton("switchbutton", "Click to swtich")
                   )
                 )
        ),
        tabPanel("T2", value = "2", 
                 fluidRow(
                   box(width = 9, "Some different content here")
                 )
        )
      )
    )
  )
)

server <-  function(input, output, session) {
  observeEvent(input$switchbutton, {
    updateTabsetPanel(session = session,
                      inputId = "tabset2",
                      selected = "2")
  })
}

shinyApp(ui, server)

# Modular version
library(shiny)
library(shinydashboard)

switcherButton <- function(id) {
  ns <- NS(id)
  fluidRow(
    column(
      width = 12,
      tabBox(
        id = "tabset2",
        type = "tabs",
        selected = "1",
        tabPanel("T1", value = "1", 
                 fluidRow(
                   box(width = 9, "Some content here"),
                   box(width = 3, 
                       actionButton(ns("switchbutton"), "Click to switch")
                   )
                 )
        ),
        tabPanel("T2", value = "2", 
                 fluidRow(
                   box(width = 9, "Some different content here")
                 )
        )
      )
    )
  )
}

switcher <- function(input, output, session, parent) {
  observeEvent(input$switchbutton, {
    updateTabsetPanel(session = session,
                      inputId = "tabset2",
                      selected = "2")
  })
}

ui <- fluidPage(
  switcherButton("switcher1")
)

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

  callModule(switcher, "switcher1")

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny module shinydashboard


    【解决方案1】:

    您忘记将tabBox id 包装在ns() 中。只需将"tabset2" 替换为ns("tabset2")

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2022-08-17
      • 1970-01-01
      • 2018-08-05
      相关资源
      最近更新 更多