gpt4 book ai didi

r - 如何从模块内部切换 Shiny 的选项卡面板?

转载 作者:行者123 更新时间:2023-12-04 17:28:01 26 4
gpt4 key购买 nike

我正在尝试模块化一个 Shiny 的应用程序,它有一个按钮,可以使用 updateTabsetPanel() 在 tabPanel 之间进行响应式(Reactive)切换。这在非模块化应用程序中有效,但在使 react 包含在模块中时无效。我认为这与模块范围之外的面板有关。下面是最小的例子(非模块化版本有效,模块化版本无效)。有没有办法让模块与 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)

最佳答案

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

关于r - 如何从模块内部切换 Shiny 的选项卡面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62135166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com