Shiny 中不同选项卡中 selectizeInput 的持久状态

Persistent states of selectizeInput across different tabs in Shiny

我有以下 Shiny 应用程序,其中有 parent 和 child 选项卡。单击 child 选项卡会创建具有不同默认值的 selectizeInput。 正如您在代码中看到的那样,所有 child selectizeInput 的 ID 都是相同的,而且此控件仍然为每个 child selectizeInput.[=17= 存储不同的状态]

我想为所有 child 选项卡创建一个 selectizeInput 并且此 selectizeInput 的值在所有选项卡中应该是持久的/相同的。

请让我知道我做错了什么。

library(shiny)
library(shinydashboard)

sidebar <- dashboardSidebar(
  collapsed = FALSE,
  sidebarMenu(id = "menu_sidebar",
              conditionalPanel(
                # https://community.rstudio.com/t/shiny-with-tabsetpanels-and-conditionpanel-not-hiding-what-it-should/57159/3
                condition = "input.main_tab == 'mother tab' && input.group_tab == 't_child1'",
                selectizeInput(inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = "APPLE")
              ),
              conditionalPanel(
                condition = "input.main_tab == 'mother tab' && input.group_tab == 't_child2'",
                selectizeInput(inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = "ORANGE")
              ),
              conditionalPanel(
                condition = "input.main_tab == 'mother tab' && input.group_tab == 't_child3'",
                selectizeInput(inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = "PEAR")
              ),
              conditionalPanel(
                condition = "input.main_tab == 'tab 1'",
                selectizeInput(inputId = "t1", label = "Select by:", choices = c(as.character(30:40)))
              ),
              conditionalPanel(
                condition = "input.main_tab == 'tab 2'",
                selectizeInput(inputId = "t2", label = "Select by:", choices = c(as.character(40:50)))
              )
  )
)


body <- dashboardBody(
  fluidRow(
    tabsetPanel(id = "main_tab",
                selected = "mother tab",
                tabPanel(title = "tab 1", "Tab content 1"),
                tabPanel(title = "tab 2", "Tab content 2"),
                tabPanel(title = "mother tab",
                         tabsetPanel(type = "tabs", id = "group_tab", selected = "Tab_child_2",
                                     tabPanel(title = "child_1", value = "t_child1", "Tab child content 1"),
                                     tabPanel(title = "child_2", value = "t_child2", "Tab child content 2"),
                                     tabPanel(title = "child_3", value = "t_child3", "Tab child content 3")
                         )
                )
                
    )
  )
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    sidebar,
    body
  ),
  server = function(input, output) {
  }
)

我会将逻辑转移到服务器端,因为 UI 部分只在启动时呈现一次:

library(shiny)
library(shinydashboard)

sidebar <- dashboardSidebar(
  collapsed = FALSE,
  sidebarMenu(id = "menu_sidebar",
              selectizeInput(inputId = "tc", label = "Select by:", choices = NULL)
  )
)

body <- dashboardBody(
  fluidRow(
    tabsetPanel(id = "main_tab",
                selected = "mother tab",
                tabPanel(title = "tab 1", "Tab content 1"),
                tabPanel(title = "tab 2", "Tab content 2"),
                tabPanel(title = "mother tab",
                         tabsetPanel(type = "tabs", id = "group_tab", selected = "Tab_child_2",
                                     tabPanel(title = "child_1", value = "t_child1", "Tab child content 1"),
                                     tabPanel(title = "child_2", value = "t_child2", "Tab child content 2"),
                                     tabPanel(title = "child_3", value = "t_child3", "Tab child content 3")
                         )
                )
    )
  )
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    sidebar,
    body
  ),
  server = function(input, output, session) {
    observeEvent(c(req(input$main_tab), input$group_tab), {
      
      if (input$main_tab == 'tab 1'){
        updateSelectizeInput(session, inputId = "tc", label = "Select by:", choices = c(as.character(30:40)), selected = isolate(input$tc))
      } else if (input$main_tab == 'tab 2'){
        updateSelectizeInput(session, inputId = "tc", label = "Select by:", choices = c(as.character(40:50)), selected = isolate(input$tc))
      }
      
      req(input$group_tab)
      if(input$main_tab == 'mother tab' && input$group_tab == 't_child1'){
        updateSelectizeInput(session, inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = isolate(input$tc))
      } else if (input$main_tab == 'mother tab' && input$group_tab == 't_child2'){
        updateSelectizeInput(session, inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = isolate(input$tc))
      } else if (input$main_tab == 'mother tab' && input$group_tab == 't_child3'){
        updateSelectizeInput(session, inputId = "tc", label = "Select by:", choices = c("APPLE", "ORANGE", "PEAR"), selected = isolate(input$tc))
      }
      
    })
  }
)