You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny模态框点击Auth按钮无法关闭的解决方法

解决Shiny模态框点击Auth按钮无法关闭的问题

问题出在你直接在UI里静态渲染了模态框,Shiny的removeModal()函数只对通过showModal()动态触发的模态框生效,静态渲染的模态框不在Shiny的模态框管理体系内,所以点击按钮后无法关闭。

修改方案:

  • 不在UI中直接调用fmodal(),而是在server端通过showModal()在应用启动时显示模态框
  • 保留原有的observeEvent逻辑即可

修改后的完整代码:

library(shiny); library(shinydashboard)

fmodal <- function(){
  modalDialog(
    id = "authModal",
    title = "title", 
    textOutput("modalMessage"), 
    textInput("username", "username", value = "username"), 
    passwordInput("password", "password", value = "password"), 
    footer = tagList(
      actionButton("b_auth", "Auth"), 
      modalButton("Cancel")
    )
  )
}

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"), 
  dashboardSidebar(
    sidebarMenu(
      menuItem("Accueil", tabName = "dashboard", icon = icon("dashboard"))
    )
  ), 
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard")
    )
  )
)

server <- function(input, output, session) {
  # 应用启动时显示模态框
  showModal(fmodal())
  
  observeEvent(input$b_auth, {
    removeModal() 
  })
}

shinyApp(ui = ui, server = server)

内容的提问来源于stack exchange,提问作者dia05

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:05:05