Debian Codegraph @

R/input-action.R

raw
1 #' Action button/link
2 #'
3 #' Creates an action button or link whose value is initially zero, and increments by one
4 #' each time it is pressed.
5 #'
6 #' @inheritParams textInput
7 #' @param label The contents of the button or link--usually a text label, but
8 #' you could also use any other HTML, like an image.
9 #' @param icon An optional [icon()] to appear on the button.
10 #' @param disabled If `TRUE`, the button will not be clickable. Use
11 #' [updateActionButton()] to dynamically enable/disable the button.
12 #' @param ... Named attributes to be applied to the button or link.
13 #'
14 #' @family input elements
15 #' @examples
16 #' ## Only run examples in interactive R sessions
17 #' if (interactive()) {
18 #'
19 #' ui <- fluidPage(
20 #' sliderInput("obs", "Number of observations", 0, 1000, 500),
21 #' actionButton("goButton", "Go!", class = "btn-success"),
22 #' plotOutput("distPlot")
23 #' )
24 #'
25 #' server <- function(input, output) {
26 #' output$distPlot <- renderPlot({
27 #' # Take a dependency on input$goButton. This will run once initially,
28 #' # because the value changes from NULL to 0.
29 #' input$goButton
30 #'
31 #' # Use isolate() to avoid dependency on input$obs
32 #' dist <- isolate(rnorm(input$obs))
33 #' hist(dist)
34 #' })
35 #' }
36 #'
37 #' shinyApp(ui, server)
38 #'
39 #' }
40 #'
41 #' ## Example of adding extra class values
42 #' actionButton("largeButton", "Large Primary Button", class = "btn-primary btn-lg")
43 #' actionLink("infoLink", "Information Link", class = "btn-info")
44 #'
45 #' @seealso [observeEvent()] and [eventReactive()]
46 #'
47 #' @section Server value:
48 #' An integer of class `"shinyActionButtonValue"`. This class differs from
49 #' ordinary integers in that a value of 0 is considered "falsy".
50 #' This implies two things:
51 #' * Event handlers (e.g., [observeEvent()], [eventReactive()]) won't execute on initial load.
52 #' * Input validation (e.g., [req()], [need()]) will fail on initial load.
53 #' @export
54 actionButton <- function(inputId, label, icon = NULL, width = NULL,
55 disabled = FALSE, ...) {
56
57 value <- restoreInput(id = inputId, default = NULL)
58
59 icon <- validateIcon(icon)
60
61 if (!is.null(icon)) {
62 icon <- span(icon, class = "action-icon", .noWS = c("outside", "inside"))
63 }
64
65 if (!is.null(label)) {
66 label <- span(label, class = "action-label", .noWS = c("outside", "inside"))
67 }
68
69 tags$button(
70 id = inputId,
71 style = css(width = validateCssUnit(width)),
72 type = "button",
73 class = "btn btn-default action-button",
74 `data-val` = value,
75 disabled = if (isTRUE(disabled)) NA else NULL,
76 icon, label,
77 .noWS = "inside",
78 ...
79 )
80 }
81
82 #' @rdname actionButton
83 #' @export
84 actionLink <- function(inputId, label, icon = NULL, ...) {
85 value <- restoreInput(id = inputId, default = NULL)
86
87 icon <- validateIcon(icon)
88
89 if (!is.null(icon)) {
90 icon <- span(icon, class = "action-icon", .noWS = c("outside", "inside"))
91 }
92
93 if (!is.null(label)) {
94 label <- span(label, class = "action-label", .noWS = c("outside", "inside"))
95 }
96
97 tags$a(
98 id = inputId,
99 href = "#",
100 class = "action-button action-link",
101 `data-val` = value,
102 icon, label,
103 .noWS = "inside",
104 ...
105 )
106 }
107
108
109 # Throw an informative warning if icon isn't html-ish
110 validateIcon <- function(icon) {
111 if (length(icon) == 0) {
112 return(icon)
113 }
114 if (!isTagLike(icon)) {
115 rlang::warn(
116 c(
117 "It appears that a non-HTML value was provided to `icon`.",
118 i = "Try using a `shiny::icon()` (or an equivalent) to get an icon."
119 ),
120 class = "shiny-validate-icon"
121 )
122 }
123 icon
124 }