您如何根据闪亮的选定 tabItem() 执行不同的服务器?

How do you have different server execution based on selected tabItem() in shiny?

背景 我正在使用 {brochure} 和 {golem} 构建一个闪亮的应用程序。我有一个外部模块 grid,它由内部模块 subGrid2 组成,在两个选项卡上显示相同的模块 UI。

目标

问题

observeEvent 反应表达式。无法识别何时选择了相应的选项卡以生成正确的服务器响应。

-使用下面的 reprex 复制了我的问题-

TL/DR

  1. 为什么 observeEvent 反应式不会为每个选定的选项卡生成正确的服务器响应?

REPREX

取消注释 observeEvent 以查看错误

#22.2.22
library(brochure)

library(shiny)

library(shinipsum)

library(shinydashboard)

library(shinydashboardPlus)

mod_subGrid2_ui <- function(id) {
    ns <- NS(id)
    tagList(
        plotOutput(ns("plot"))
    )
}

mod_subGrid2_server <- function(id) {
    moduleServer(id, function(input, output, session) {
        ns <- session$ns

        output$plot <- renderPlot({
            shinipsum::random_ggplot()
        })
    })
}

#Setup dashboard

mod_Grid_ui <- function(id) {
    ns <- NS(id)

    shinydashboardPlus::dashboardPage(
        skin = "midnight",
        header = dashboardHeader(title = "test"),
        sidebar = dashboardSidebar(
            shinydashboard::sidebarMenu(
                # Setting id makes input$tabs give the tabName of currently-selected tab
                id = "tabs",
                menuItem("Home", tabName = "home", icon = icon("tachometer-alt")),
                menuItem("Portfolio", tabName = "portfolio", icon = icon("chart-line"), badgeLabel = "new",
                         badgeColor = "green")
            )
        ),

        body = shinydashboard::dashboardBody(
            # Enable shinyjs
            shinyjs::useShinyjs(),

            shinydashboard::tabItems(
                shinydashboard::tabItem("home",
                                        shiny::tagList(
                                            div(p("Content for 1st tab goes here -- GRID MODULE")),
                                            mod_subGrid2_ui(ns("subGrid2_ui_1"))
                                        )
                ),
                shinydashboard::tabItem("portfolio",
                                        shiny::tagList(
                                            div(p("Content for 2nd goes here -- GRID MODULE (2x)")),
                                            titlePanel(title = "The same module UI goes here"),

mod_subGrid2_ui(ns("subGrid2_ui_2"))
                                        )
                )
            )
        )
    )
}

mod_Grid_server <- function(id) {
    moduleServer(id, function(input, output, session) {
        ns <- session$ns
        mod_subGrid2_server("subGrid2_ui_1")
        mod_subGrid2_server("subGrid2_ui_2")

       ## uncomment to try
        # observeEvent(input$tabs,{
        #   if(input$tabs == "home"){
        #     # <subGrid> server fragment
        #     mod_subGrid2_server("subGrid2_ui_1")
        #   } else if(input$tabs == "portfolio"){
        #     mod_subGrid2_server("subGrid2_ui_1")
        #   }
        # }, ignoreNULL = TRUE, ignoreInit = TRUE)
    })
}

brochureApp(
    page(
        href = "/",
        ui = tagList(
            mod_Grid_ui("grid_1")
        ),
        server = function(input, output, session) {
            mod_Grid_server("grid_1")
        }
    ),
    wrapped = shiny::tagList
)

当使用嵌套在另一个模块中的模块时,您需要 ns() 嵌套的 UI 函数的 id。 所以在这里,mod_subGrid2_ui(ns("subGrid2_ui_1")).

这是一个最小的代表:

mod_subGrid2_ui <- function(id) {
  ns <- NS(id)
  tagList(
    plotOutput(ns("plot"))
  )
}

mod_subGrid2_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns

    output$plot <- renderPlot({
      shinipsum::random_ggplot()
    })
  })
}

mod_Grid_ui <- function(id) {
  ns <- NS(id)
  tagList(
    mod_subGrid2_ui(ns("subGrid2_ui_1"))
  )
}

mod_Grid_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    mod_subGrid2_server("subGrid2_ui_1")
  })
}

brochureApp(
  page(
    href = "/",
    ui = tagList(
      mod_Grid_ui("grid_1")
    ),
    server = function(input, output, session) {
      mod_Grid_server("grid_1")
    }
  )
)