R语言 当你打开一个更高级别的tabsetpanel时,使一个特定的侧边栏面板自动打开的闪亮服务器函数?

vltsax25  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(118)

在下面的示例代码中,当我按下“tab2”时,是否有一个服务器函数打开侧边栏面板中的“option2”?

ui <-
  fluidPage(sidebarLayout(
  sidebarPanel(h5("sidebar:"), width = 2, style = "position:right;top:70px;",
    tabsetPanel(type = "pills",
         tabPanel(title = "option1",
                 selectInput(inputId = "dataset",
                     label = "Dataset:",
                      choices = c(
                      "Example" = "example",
                      "Examle" = "example"),
                       selected = "example")),
          tabPanel(title = "option2",
                  selectInput(inputId = "dataset",
                     label = "Dataset:",
                     choices = c(
                      "Example" = "example",
                      "example" = "example"),
                     selected = "example"),
             ))),
mainPanel(width = 10,
          navbarPage(title = "tabs",
              tabPanel("tab1",
                  fluidRow(
                     tabsetPanel(type = "pills",
                                ),
                         mainPanel(width = 12, style = "height: 100vh; overflow-y: auto;",
                             fluidRow(
                                plotOutput("plot", height = "50vh"),
                                          )))),
                     tabPanel("tab2",
                              fluidRow(
                                tabsetPanel(type = "pills",
                                ),
                         mainPanel(width = 12, style = "height: 100vh; overflow-y: auto;",
                                          fluidRow(
                                            plotOutput("plot", height = "50vh"),
                                          ))))))))
                     
server <- function(input, output)

shinyApp(ui = ui, server = server)

我尝试在服务器中使用
observeEvent()observe()使用面板的标签没有任何运气。

li9yvcax

li9yvcax1#

是的,
首先,你必须避免重复的id(datasetplot),否则什么都不能用。
然后你必须在侧边栏中为TabSet面板分配一个ID,e例如tabsetSidebar,以及主面板中导航栏页面的ID,例如tabsetSidebar。例如navbar。然后,您可以在服务器中执行以下操作:

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

  observeEvent(input[["navbar"]], {
    if(input[["navbar"]] == "tab1") {
      updateTabsetPanel(session, "tabsetSidebar", selected = "option1")
    } else {
      updateTabsetPanel(session, "tabsetSidebar", selected = "option2")
    }
  })

}

下面是带有更正的UI:

ui <-
  fluidPage(sidebarLayout(
    sidebarPanel(h5("sidebar:"),
      width = 2, style = "position:right;top:70px;",
      tabsetPanel(
        id = "tabsetSidebar",
        type = "pills",
        tabPanel(
          title = "option1",
          selectInput(
            inputId = "dataset1",
            label = "Dataset:",
            choices = c(
              "Example" = "example",
              "Examle" = "example"
            ),
            selected = "example"
          )
        ),
        tabPanel(
          title = "option2",
          selectInput(
            inputId = "dataset2",
            label = "Dataset:",
            choices = c(
              "Example" = "example",
              "example" = "example"
            ),
            selected = "example"
          ),
        )
      )
    ),
    mainPanel(
      width = 10,
      navbarPage(
        id = "navbar",
        title = "tabs",
        tabPanel(
          "tab1",
          fluidRow(
            tabsetPanel(type = "pills", ),
            mainPanel(
              width = 12, style = "height: 100vh; overflow-y: auto;",
              fluidRow(
                plotOutput("plot1", height = "50vh"),
              )
            )
          )
        ),
        tabPanel(
          "tab2",
          fluidRow(
            tabsetPanel(type = "pills", ),
            mainPanel(
              width = 12, style = "height: 100vh; overflow-y: auto;",
              fluidRow(
                plotOutput("plot2", height = "50vh"),
              )
            )
          )
        )
      )
    )
  )
)

相关问题