Skip to content

Commit

Permalink
Add formatNumericInput functions to wrap the currencyInput functions …
Browse files Browse the repository at this point in the history
…for more intuitive use with percentage and decimal formats.
  • Loading branch information
srmatth committed Aug 11, 2020
1 parent e586375 commit 702858a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 12 deletions.
43 changes: 38 additions & 5 deletions R/input-autonumeric.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@

#' Currency Input Widget
#' Format Numeric Inputs
#'
#' A shiny widget for as-you-type formatting of currency. For a more modifiable
#' version see \code{\link{autonumericInput}}.
#' Shiny widgets for as-you-type formatting of currency and numeric values. For
#' a more modifiable version see \code{\link{autonumericInput}}. These two
#' functions do the exact same thing but are named differently for more
#' intuitive use (currency for money, formatNumeric for percentage or other).
#'
#' @rdname formatNumericInput
#'
#' @param inputId The \code{input} slot that will be used to access the value.
#' @param label Display label for the control, or NULL for no label.
Expand Down Expand Up @@ -33,6 +37,8 @@
#' \item \code{"dollar"} (same as \code{"NorthAmerican"})
#' \item \code{"percentageEU2dec"}
#' \item \code{"percentageUS2dec"}
#' \item \code{"dotDecimalCharCommaSeparator"}
#' \item \code{"commaDecimalCharDotSeparator"}
#' }
#'
#' To see the full list please visit
Expand Down Expand Up @@ -62,13 +68,24 @@
#' verbatimTextOutput("res2"),
#'
#' currencyInput("id3", "Yen:", value = 1234, format = "Japanese", width = 200, align = "right"),
#' verbatimTextOutput("res3")
#' verbatimTextOutput("res3"),
#'
#' br(),
#' tags$h2("Formatted Numeric Input"),
#'
#' formatNumericInput("id4", "Numeric:", value = 1234, width = 200),
#' verbatimTextOutput("res4"),
#'
#' formatNumericInput("id5", "Percent:", value = 1234, width = 200, format = "percentageEU2dec"),
#' verbatimTextOutput("res5")
#' )
#'
#' server <- function(input, output, session) {
#' output$res1 <- renderPrint(input$id1)
#' output$res2 <- renderPrint(input$id2)
#' output$res3 <- renderPrint(input$id3)
#' output$res4 <- renderPrint(input$id4)
#' output$res5 <- renderPrint(input$id5)
#' }
#'
#' shinyApp(ui, server)
Expand Down Expand Up @@ -102,7 +119,13 @@ currencyInput <- function(inputId, label, value, format = "euro",
)
}

#' Update a Currency Input Widget
#' @rdname formatNumericInput
formatNumericInput <- function(inputId, label, value, format = "commaDecimalCharDotSeparator",
width = NULL, align = "center") {
currencyInput(inputId, label, value, format, width, align)
}

#' Update a Formatted Numeric Input Widget
#'
#' @param session Standard shiny \code{session}.
#' @param inputId The id of the input object.
Expand All @@ -111,6 +134,7 @@ currencyInput <- function(inputId, label, value, format = "euro",
#' @param format The format to change the input object to.
#'
#' @export
#' @rdname formaNumericInputUpdate
#'
#' @family autonumeric
#'
Expand Down Expand Up @@ -171,6 +195,15 @@ updateCurrencyInput <- function(session, inputId,
session$sendInputMessage(inputId, message)
}

#' @rdname formaNumericInputUpdate
updateFormatNumericInput <- function(session, inputId,
label = NULL,
value = NULL,
format = NULL) {
message <- dropNulls(list(label = label, value = value, format = format))
session$sendInputMessage(inputId, message)
}

#' Autonumeric Input Widget
#'
#' An \code{R} wrapper over the javascript \code{AutoNumeric} library, for
Expand Down
13 changes: 12 additions & 1 deletion examples/currencyInput.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ if (interactive()) {
verbatimTextOutput("res2"),

currencyInput("id3", "Yen:", value = 1234, format = "Japanese", width = 200, align = "right"),
verbatimTextOutput("res3")
verbatimTextOutput("res3"),

br(),
tags$h2("Formatted Numeric Input"),

formatNumericInput("id4", "Numeric:", value = 1234, width = 200),
verbatimTextOutput("res4"),

formatNumericInput("id5", "Percent:", value = 1234, width = 200, format = "percentageEU2dec"),
verbatimTextOutput("res5")
)

server <- function(input, output, session) {
output$res1 <- renderPrint(input$id1)
output$res2 <- renderPrint(input$id2)
output$res3 <- renderPrint(input$id3)
output$res4 <- renderPrint(input$id4)
output$res5 <- renderPrint(input$id5)
}

shinyApp(ui, server)
Expand Down
13 changes: 11 additions & 2 deletions man/updateCurrencyInput.Rd → man/formaNumericInputUpdate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions man/currencyInput.Rd → man/formatNumericInput.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 702858a

Please sign in to comment.