Debian Codegraph @

man/modalDialog.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/modal.R
3 \name{modalDialog}
4 \alias{modalDialog}
5 \alias{modalButton}
6 \title{Create a modal dialog UI}
7 \usage{
8 modalDialog(
9 ...,
10 title = NULL,
11 footer = modalButton("Dismiss"),
12 size = c("m", "s", "l", "xl"),
13 easyClose = FALSE,
14 fade = TRUE
15 )
16
17 modalButton(label, icon = NULL)
18 }
19 \arguments{
20 \item{...}{UI elements for the body of the modal dialog box.}
21
22 \item{title}{An optional title for the dialog.}
23
24 \item{footer}{UI for footer. Use \code{NULL} for no footer.}
25
26 \item{size}{One of \code{"s"} for small, \code{"m"} (the default) for medium,
27 \code{"l"} for large, or \code{"xl"} for extra large. Note that \code{"xl"} only
28 works with Bootstrap 4 and above (to opt-in to Bootstrap 4+,
29 pass \code{\link[bslib:bs_theme]{bslib::bs_theme()}} to the \code{theme} argument of a page container
30 like \code{\link[=fluidPage]{fluidPage()}}).}
31
32 \item{easyClose}{If \code{TRUE}, the modal dialog can be dismissed by
33 clicking outside the dialog box, or be pressing the Escape key. If
34 \code{FALSE} (the default), the modal dialog can't be dismissed in those
35 ways; instead it must be dismissed by clicking on a \code{modalButton()}, or
36 from a call to \code{\link[=removeModal]{removeModal()}} on the server.}
37
38 \item{fade}{If \code{FALSE}, the modal dialog will have no fade-in animation
39 (it will simply appear rather than fade in to view).}
40
41 \item{label}{The contents of the button or link--usually a text label, but
42 you could also use any other HTML, like an image.}
43
44 \item{icon}{An optional \code{\link[=icon]{icon()}} to appear on the button.}
45 }
46 \description{
47 \code{modalDialog()} creates the UI for a modal dialog, using Bootstrap's modal
48 class. Modals are typically used for showing important messages, or for
49 presenting UI that requires input from the user, such as a user name and
50 password input.
51
52 \code{modalButton()} creates a button that will dismiss the dialog when clicked,
53 typically used when customising the \code{footer}.
54 }
55 \examples{
56 if (interactive()) {
57 # Display an important message that can be dismissed only by clicking the
58 # dismiss button.
59 shinyApp(
60 ui = basicPage(
61 actionButton("show", "Show modal dialog")
62 ),
63 server = function(input, output) {
64 observeEvent(input$show, {
65 showModal(modalDialog(
66 title = "Important message",
67 "This is an important message!"
68 ))
69 })
70 }
71 )
72
73
74 # Display a message that can be dismissed by clicking outside the modal dialog,
75 # or by pressing Esc.
76 shinyApp(
77 ui = basicPage(
78 actionButton("show", "Show modal dialog")
79 ),
80 server = function(input, output) {
81 observeEvent(input$show, {
82 showModal(modalDialog(
83 title = "Somewhat important message",
84 "This is a somewhat important message.",
85 easyClose = TRUE,
86 footer = NULL
87 ))
88 })
89 }
90 )
91
92
93 # Display a modal that requires valid input before continuing.
94 shinyApp(
95 ui = basicPage(
96 actionButton("show", "Show modal dialog"),
97 verbatimTextOutput("dataInfo")
98 ),
99
100 server = function(input, output) {
101 # reactiveValues object for storing current data set.
102 vals <- reactiveValues(data = NULL)
103
104 # Return the UI for a modal dialog with data selection input. If 'failed' is
105 # TRUE, then display a message that the previous value was invalid.
106 dataModal <- function(failed = FALSE) {
107 modalDialog(
108 textInput("dataset", "Choose data set",
109 placeholder = 'Try "mtcars" or "abc"'
110 ),
111 span('(Try the name of a valid data object like "mtcars", ',
112 'then a name of a non-existent object like "abc")'),
113 if (failed)
114 div(tags$b("Invalid name of data object", style = "color: red;")),
115
116 footer = tagList(
117 modalButton("Cancel"),
118 actionButton("ok", "OK")
119 )
120 )
121 }
122
123 # Show modal when button is clicked.
124 observeEvent(input$show, {
125 showModal(dataModal())
126 })
127
128 # When OK button is pressed, attempt to load the data set. If successful,
129 # remove the modal. If not show another modal, but this time with a failure
130 # message.
131 observeEvent(input$ok, {
132 # Check that data object exists and is data frame.
133 if (!is.null(input$dataset) && nzchar(input$dataset) &&
134 exists(input$dataset) && is.data.frame(get(input$dataset))) {
135 vals$data <- get(input$dataset)
136 removeModal()
137 } else {
138 showModal(dataModal(failed = TRUE))
139 }
140 })
141
142 # Display information about selected data
143 output$dataInfo <- renderPrint({
144 if (is.null(vals$data))
145 "No data selected"
146 else
147 summary(vals$data)
148 })
149 }
150 )
151 }
152 }