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
相关产品推荐
相关产品推荐

