Debian Codegraph @

R/input-numeric.R

raw
1 #' Create a numeric input control
2 #'
3 #' Create an input control for entry of numeric values
4 #'
5 #' @inheritParams textInput
6 #' @param min Minimum allowed value
7 #' @param max Maximum allowed value
8 #' @param step Interval to use when stepping between min and max
9 #' @return A numeric input control that can be added to a UI definition.
10 #'
11 #' @family input elements
12 #' @seealso [updateNumericInput()]
13 #'
14 #' @examples
15 #' ## Only run examples in interactive R sessions
16 #' if (interactive()) {
17 #'
18 #' ui <- fluidPage(
19 #' numericInput("obs", "Observations:", 10, min = 1, max = 100),
20 #' verbatimTextOutput("value")
21 #' )
22 #' server <- function(input, output) {
23 #' output$value <- renderText({ input$obs })
24 #' }
25 #' shinyApp(ui, server)
26 #' }
27 #'
28 #' @section Server value:
29 #' A numeric vector of length 1.
30 #'
31 #' @export
32 numericInput <- function(
33 inputId,
34 label,
35 value,
36 min = NA,
37 max = NA,
38 step = NA,
39 width = NULL,
40 ...,
41 updateOn = c("change", "blur")
42 ) {
43 rlang::check_dots_empty()
44 updateOn <- rlang::arg_match(updateOn)
45
46 value <- restoreInput(id = inputId, default = value)
47
48 # build input tag
49 inputTag <- tags$input(
50 id = inputId,
51 type = "number",
52 class = "shiny-input-number form-control",
53 value = formatNoSci(value),
54 `data-update-on` = updateOn
55 )
56 if (!is.na(min)) inputTag$attribs$min = min
57 if (!is.na(max)) inputTag$attribs$max = max
58 if (!is.na(step)) inputTag$attribs$step = step
59
60 div(
61 class = "form-group shiny-input-container",
62 style = css(width = validateCssUnit(width)),
63 shinyInputLabel(inputId, label),
64 inputTag
65 )
66 }