From 702858a1bdf147510ca89e00efefb074f4d00222 Mon Sep 17 00:00:00 2001 From: srmatth Date: Tue, 11 Aug 2020 11:11:39 -0600 Subject: [PATCH] Add formatNumericInput functions to wrap the currencyInput functions for more intuitive use with percentage and decimal formats. --- R/input-autonumeric.R | 43 ++++++++++++++++--- examples/currencyInput.R | 13 +++++- ...ncyInput.Rd => formaNumericInputUpdate.Rd} | 13 +++++- ...currencyInput.Rd => formatNumericInput.Rd} | 33 ++++++++++++-- 4 files changed, 90 insertions(+), 12 deletions(-) rename man/{updateCurrencyInput.Rd => formaNumericInputUpdate.Rd} (88%) rename man/{currencyInput.Rd => formatNumericInput.Rd} (72%) diff --git a/R/input-autonumeric.R b/R/input-autonumeric.R index a86b31a0..fc4cbed8 100644 --- a/R/input-autonumeric.R +++ b/R/input-autonumeric.R @@ -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. @@ -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 @@ -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) @@ -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. @@ -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 #' @@ -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 diff --git a/examples/currencyInput.R b/examples/currencyInput.R index 58ca7726..9250d9d0 100644 --- a/examples/currencyInput.R +++ b/examples/currencyInput.R @@ -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) diff --git a/man/updateCurrencyInput.Rd b/man/formaNumericInputUpdate.Rd similarity index 88% rename from man/updateCurrencyInput.Rd rename to man/formaNumericInputUpdate.Rd index e2681a39..38eaa4f0 100644 --- a/man/updateCurrencyInput.Rd +++ b/man/formaNumericInputUpdate.Rd @@ -2,7 +2,8 @@ % Please edit documentation in R/input-autonumeric.R \name{updateCurrencyInput} \alias{updateCurrencyInput} -\title{Update a Currency Input Widget} +\alias{updateFormatNumericInput} +\title{Update a Formatted Numeric Input Widget} \usage{ updateCurrencyInput( session, @@ -11,6 +12,14 @@ updateCurrencyInput( value = NULL, format = NULL ) + +updateFormatNumericInput( + session, + inputId, + label = NULL, + value = NULL, + format = NULL +) } \arguments{ \item{session}{Standard shiny \code{session}.} @@ -24,7 +33,7 @@ updateCurrencyInput( \item{format}{The format to change the input object to.} } \description{ -Update a Currency Input Widget +Update a Formatted Numeric Input Widget } \examples{ if (interactive()) { diff --git a/man/currencyInput.Rd b/man/formatNumericInput.Rd similarity index 72% rename from man/currencyInput.Rd rename to man/formatNumericInput.Rd index bd8a0783..7b3f4352 100644 --- a/man/currencyInput.Rd +++ b/man/formatNumericInput.Rd @@ -2,7 +2,8 @@ % Please edit documentation in R/input-autonumeric.R \name{currencyInput} \alias{currencyInput} -\title{Currency Input Widget} +\alias{formatNumericInput} +\title{Format Numeric Inputs} \usage{ currencyInput( inputId, @@ -12,6 +13,15 @@ currencyInput( width = NULL, align = "center" ) + +formatNumericInput( + inputId, + label, + value, + format = "commaDecimalCharDotSeparator", + width = NULL, + align = "center" +) } \arguments{ \item{inputId}{The \code{input} slot that will be used to access the value.} @@ -33,8 +43,10 @@ input. See "Details" for possible values.} a currency input widget that can be added to the UI of a shiny app. } \description{ -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). } \details{ In regards to \code{format}, there are currently 41 sets of predefined @@ -55,6 +67,8 @@ The most common are: \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 @@ -76,13 +90,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)