R Shiny弹窗在标签页结构中无法触发显示的问题求助
解决Shiny中引入tabPanel后bsModal弹窗无法触发的问题
你的问题源于UI结构层级错误:非工作代码里的tabPanel和navbarPage是平级放在fluidPage中的,这导致bsModal无法正确关联到tabPanel内的触发按钮go。navbarPage的内容必须作为其内部参数定义,外部的tabPanel不会被纳入导航栏的结构体系,进而破坏了组件间的关联逻辑。
修正后的完整代码
library(shiny) library(shinyBS) ui = fluidPage( navbarPage( id="navid", collapsible = TRUE, position = "static-top", fluid = TRUE, windowTitle = "Test", theme=bslib::bs_theme(bootswatch="flatly"), "Some Test", # 将tabPanel移到navbarPage内部,作为导航栏的标签页 tabPanel('Hist', fluidPage( sidebarLayout( sidebarPanel(numericInput("n", "n", 50), actionButton("go", "Go")), mainPanel( bsModal("modalExample", "Your plot", "go", size = "large",plotOutput("plot")) ) )) ) ) ) server = function(input, output, session) { randomVals <- eventReactive(input$go, { runif(input$n) }) output$plot <- renderPlot({ hist(randomVals()) }) } shinyApp(ui, server)
关键改动说明
把原本独立在navbarPage之外的tabPanel,移动到navbarPage的括号内部,让它成为导航栏的一个标签页内容。这样模态框和触发按钮处于同一正确的层级结构下,bsModal就能正常识别并响应按钮的点击事件了。
内容的提问来源于stack exchange,提问作者Working dollar
相关产品推荐
相关产品推荐

