Learn R Programming

shinyvalidate

Overview

shinyvalidate adds input validation capabilities to Shiny.

Installation

Install the latest CRAN release:

install.packages("shinyvalidate")

Or, you can install the latest development version from GitHub using the remotes package.

remotes::install_github("rstudio/shinyvalidate")

Basic usage

To add validation to your Shiny app, you need to:

  1. Create an InputValidator object: iv <- InputValidator$new()

  2. Add one or more validation rules to the InputValidator: iv$add_rule("title", sv_required())

  3. Turn the validator on: iv$enable()

That's all you need to do to get validation messages to show up. Here is a simple example:

library(shiny)
library(shinyvalidate)

ui <- fluidPage(
  textInput("name", "Name"),
  textInput("email", "Email")
)

server <- function(input, output, session) {
  iv <- InputValidator$new()
  iv$add_rule("name", sv_required())
  iv$add_rule("email", sv_required())
  iv$add_rule("email", sv_email())
  iv$enable()
}

shinyApp(ui, server)

To learn about other features of shinyvalidate, including deferred validation, programmatically accessing validation results, and validating Shiny modules, see Introduction to shinyvalidate.

Input widget compatibility

shinyvalidate should work with all of the inputs that are included in Shiny. It will also work with most custom inputs that follow Bootstrap 3 or 4 conventions. Other types of custom inputs can include their own logic for displaying shinyvalidate error messages; see the article Displaying validation errors to learn more.

Prior art

  • Shiny's built-in validation. The shiny::validate() function fits naturally with Shiny's reactive programming model, but has limited usefulness because it only shows validation feedback in downstream reactive outputs, instead of providing the feedback next to the incorrect input, where users expect it.

  • shinyFeedback by @merlinoa, who graciously provided feedback on the design of shinyvalidate. Compared to shinyFeedback, shinyvalidate aims to have a more concise but less flexible R API; and on the UI side, shinyFeedback displays richer feedback on a hard-coded set of components, while shinyvalidate feedback is minimalist but aims to support a larger set of components and is extensible for custom inputs.

Copy Link

Version

Install

install.packages('shinyvalidate')

Monthly Downloads

7,071

Version

0.1.3

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Carson Sievert

Last Published

October 4th, 2023

Functions in shinyvalidate (0.1.3)

sv_integer

Validate that a field is a number that is integer-like
InputValidator

Shiny validation object
sv_lt

Validate that a field is less than a specified value
skip_validation

Skip any normal validation performed by a rule
compose_rules

Combine shinyvalidate rule functions
sv_numeric

Validate that a field is a number
sv_gte

Validate that a field is greater than or equal to a specified value
sv_email

Validate that a field contains an email address
sv_in_set

Validate that a field is part of a defined set
sv_regex

Validate that a field matches a regular expression
sv_lte

Validate that a field is less than or equal to a specified value
sv_required

Validate that the field is present
sv_url

Validate that a field contains a URL
sv_optional

Indicate that a field is optional
sv_not_equal

Validate that a field is not equal to a specified value
input_provided

Check whether an input value has been provided
sv_between

Validate that a field is a number bounded by minimum and maximum values
sv_equal

Validate that a field is equal to a specified value
sv_gt

Validate that a field is greater than a specified value