The sv_numeric() function validates that a field is numeric with the base::is.numeric() function. By default, only a single, finite, not-missing, valid number is allowed, but each of those criteria can be controlled via arguments.

sv_numeric(
  message = "A number is required",
  allow_multiple = FALSE,
  allow_na = FALSE,
  allow_nan = FALSE,
  allow_inf = FALSE
)

Arguments

message

The validation error message to use if a value is not numeric.

allow_multiple

If FALSE (the default), then the length of the input vector must be exactly one; if TRUE, then any length is allowed (including a length of zero; use sv_required() if one or more values should be required).

allow_na, allow_nan

If FALSE (the default for both options), then any NA or NaN element will cause validation to fail.

allow_inf

If FALSE (the default), then any Inf or -Inf element will cause validation to fail.

Value

A function suitable for use as an InputValidator$add_rule() rule.

See also

The sv_integer() function, which tests whether a field value is a number that is integer-like.

Other rule functions: compose_rules(), sv_between(), sv_email(), sv_equal(), sv_gte(), sv_gt(), sv_in_set(), sv_integer(), sv_lte(), sv_lt(), sv_not_equal(), sv_optional(), sv_regex(), sv_required(), sv_url()

Examples

## Only run examples in interactive R sessions
if (interactive()) {

library(shiny)
library(shinyvalidate)

ui <- fluidPage(
  textInput("rating", "Rating")
)

server <- function(input, output, session) {
  
  # Validation rules are set in the server, start by
  # making a new instance of an `InputValidator()`
  iv <- InputValidator$new()

  # Basic usage: `sv_numeric()` works well with its
  # defaults; a message will be displayed if the
  # validation of `input$rating` fails
  iv$add_rule("rating", sv_numeric())

  # Finally, `enable()` the validation rules
  iv$enable()
}

shinyApp(ui, server)

}