目标
点击一个按钮切换到一个特定的标签在shiny .
工作示例
这是可行的:
library(bslib)
library(shiny)
ui <- bslib::page_navbar(
title = "App Title",
id = "titlePage",
navset_tab(
id = "tabs",
nav_panel(
"one",
textInput(
'tabOneText',
label = "You're on tab 1"
),
actionButton(
inputId = "ac",
"Select your tab"
),
verbatimTextOutput("boo")
),
nav_panel(
"two",
textInput(
"tabTwoText",
label = "You're on tab 2"
)
)
)
)
server <- function(input, output, session){
observeEvent(input$ac,{
if (input$ac>0){
nav_select(
"tabs",
selected = "two"
)
}
})
output$boo <- renderPrint({
input$ac
})
}
shinyApp(ui, server)
不使用模块
当我使用模块时,面板switch 不工作:
library(bslib)
library(shiny)
my_module_ui <- function(id){
ns <- NS(id)
tagList(
actionButton(
inputId = ns("ac"),
"Select your tab"
),
verbatimTextOutput(ns("boo"))
)
}
my_module_server <- function(id){
moduleServer(id, function(input, output, session) {
observeEvent(input$ac,{
if (input$ac>0){
nav_select(
"tabs",
selected = "two"
)
}
})
output$boo <- renderPrint({
input$ac
})
}
)
}
ui <- bslib::page_navbar(
title = "App Title",
id = "titlePage",
navset_tab(
id = "tabs",
nav_panel(
"one",
textInput(
'tabOneText',
label = "You're on tab 1"
),
my_module_ui("btn")
),
nav_panel(
"two",
textInput(
"tabTwoText",
label = "You're on tab 2"
)
)
)
)
server <- function(input, output, session){
my_module_server("btn")
}
shinyApp(ui, server)
如何告知系统切换到选中页签?