Skip to contents

Create a Bootstrap 386 modal

Show a Bootstrap 386 modal

Hide a Bootstrap 386 modal

Usage

modal_386(
  ...,
  title = NULL,
  footer = modalButton("Dismiss"),
  size = c("m", "s", "l", "xl"),
  easyClose = FALSE,
  fade = TRUE
)

show_modal_386(ui, session = getDefaultReactiveDomain())

remove_modal_386(session = getDefaultReactiveDomain())

Arguments

...

UI elements for the body of the modal dialog box.

title

An optional title for the dialog.

UI for footer. Use NULL for no footer.

size

One of "s" for small, "m" (the default) for medium, "l" for large, or "xl" for extra large. Note that "xl" only works with Bootstrap 4 and above (to opt-in to Bootstrap 4+, pass bslib::bs_theme() to the theme argument of a page container like fluidPage()).

easyClose

If TRUE, the modal dialog can be dismissed by clicking outside the dialog box, or be pressing the Escape key. If FALSE (the default), the modal dialog can't be dismissed in those ways; instead it must be dismissed by clicking on a modalButton(), or from a call to removeModal() on the server.

fade

If FALSE, the modal dialog will have no fade-in animation (it will simply appear rather than fade in to view).

ui

UI content to show in the modal.

session

The session object passed to function given to shinyServer.

Examples

if (interactive()) {
 library(shiny)
 library(shiny386)

 shinyApp(
  ui = page_386(
    button_386("show", "Show modal dialog"),
    verbatimTextOutput("dataInfo")
  ),

  server = function(input, output) {
    # reactiveValues object for storing current data set.
    vals <- reactiveValues(data = NULL)

   # Return the UI for a modal dialog with data selection input. If 'failed' is
   # TRUE, then display a message that the previous value was invalid.
   dataModal <- function(failed = FALSE) {
     modal_386(
       textInput("dataset", "Choose data set",
                 placeholder = 'Try "mtcars" or "abc"'
       ),
       span('(Try the name of a valid data object like "mtcars", ',
            'then a name of a non-existent object like "abc")'),
       if (failed)
         div(tags$b("Invalid name of data object", style = "color: red;")),

       footer = tagList(
         modalButton("Cancel"),
         button_386("ok", "OK")
       )
     )
   }

   # Show modal when button is clicked.
   observeEvent(input$show, {
     show_modal_386(dataModal())
   })

   # When OK button is pressed, attempt to load the data set. If successful,
   # remove the modal. If not show another modal, but this time with a failure
   # message.
   observeEvent(input$ok, {
     # Check that data object exists and is data frame.
     if (!is.null(input$dataset) && nzchar(input$dataset) &&
         exists(input$dataset) && is.data.frame(get(input$dataset))) {
       vals$data <- get(input$dataset)
       remove_modal_386()
     } else {
       show_modal_386(dataModal(failed = TRUE))
     }
   })

   # Display information about selected data
   output$dataInfo <- renderPrint({
     if (is.null(vals$data))
       "No data selected"
     else
       summary(vals$data)
   })
  }
 )
}