R Shiny: Present a ShinyBS Modal Popup on page visit (no user action)

后端 未结 2 2137
太阳男子
太阳男子 2020-12-19 22:49

I used bsModal successfully in my code before. However, I can\'t seem to get a modal pop up to show just when the user visits an app\'s first page by default. I thought some

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 23:27

    Here is a solution using JS to trigger bsModal when page load "onload" from ui without waiting for the server. Along with a solution proposed here to prevent end users from accidentally closing the modal by clicking outside the modal or press Esc

    library(shiny)
    library(shinyBS)
    bsModalNoClose <-function(...) {
         b = bsModal(...)
         b[[2]]$`data-backdrop` = "static"
         b[[2]]$`data-keyboard` = "false"
      return(b)
    }
    
    
    ui <- fluidPage(
           sidebarLayout(
              sidebarPanel(
                  bsModalNoClose("window", "Window",
                      title="Enter Login Details",size='small',
                      textInput('username', 'Username'),
                      passwordInput('pwInp', 'Password'),
                      actionButton('butLogin', 'Login', class = 'btn action-button btn-success', icon = icon('sign-in')),
                      footer = h4(actionLink('create_account','Create an account'),align='right'),
                      tags$head(tags$style("#window .modal-footer{display:none}
                                           .modal-header .close{display:none}"),
                                tags$script("$(document).ready(function(){
                                            $('#window').modal();
                                            });")
                                ))
                      )
            ,mainPanel()
      ))
    
    server <- function(input, output, session) {}
    
    shinyApp(ui, server)
    

    I hope it may be helpful for future readers.

提交回复
热议问题