Debian Codegraph @

R/input-submit.R

raw
1 #' Create a submit button
2 #'
3 #' Create a submit button for an app. Apps that include a submit
4 #' button do not automatically update their outputs when inputs change,
5 #' rather they wait until the user explicitly clicks the submit button.
6 #' The use of `submitButton` is generally discouraged in favor of
7 #' the more versatile [actionButton()] (see details below).
8 #'
9 #' Submit buttons are unusual Shiny inputs, and we recommend using
10 #' [actionButton()] instead of `submitButton` when you
11 #' want to delay a reaction.
12 #' See [this
13 #' article](https://shiny.rstudio.com/articles/action-buttons.html) for more information (including a demo of how to "translate"
14 #' code using a `submitButton` to code using an `actionButton`).
15 #'
16 #' In essence, the presence of a submit button stops all inputs from
17 #' sending their values automatically to the server. This means, for
18 #' instance, that if there are *two* submit buttons in the same app,
19 #' clicking either one will cause all inputs in the app to send their
20 #' values to the server. This is probably not what you'd want, which is
21 #' why submit button are unwieldy for all but the simplest apps. There
22 #' are other problems with submit buttons: for example, dynamically
23 #' created submit buttons (for example, with [renderUI()]
24 #' or [insertUI()]) will not work.
25 #'
26 #' @param text Button caption
27 #' @param icon Optional [icon()] to appear on the button
28 #' @param width The width of the button, e.g. `'400px'`, or `'100%'`;
29 #' see [validateCssUnit()].
30 #' @return A submit button that can be added to a UI definition.
31 #'
32 #' @family input elements
33 #'
34 #' @examples
35 #' if (interactive()) {
36 #'
37 #' shinyApp(
38 #' ui = basicPage(
39 #' numericInput("num", label = "Make changes", value = 1),
40 #' submitButton("Update View", icon("refresh")),
41 #' helpText("When you click the button above, you should see",
42 #' "the output below update to reflect the value you",
43 #' "entered at the top:"),
44 #' verbatimTextOutput("value")
45 #' ),
46 #' server = function(input, output) {
47 #'
48 #' # submit buttons do not have a value of their own,
49 #' # they control when the app accesses values of other widgets.
50 #' # input$num is the value of the number widget.
51 #' output$value <- renderPrint({ input$num })
52 #' }
53 #' )
54 #' }
55 #' @export
56 submitButton <- function(text = "Apply Changes", icon = NULL, width = NULL) {
57 div(
58 tags$button(
59 type="submit",
60 class="btn btn-primary shiny-submit-button",
61 style = css(width = validateCssUnit(width)),
62 list(icon, text)
63 )
64 )
65 }