validate() provides convenient mechanism for validating that an output has all the inputs necessary for successful rendering. It takes any number of (unnamed) arguments, each representing a condition to test. If any of condition fails (i.e. is not "truthy"), a special type of error is signaled to stop execution. If this error is not handled by application-specific code, it is displayed to the user by Shiny.

If you use validate() in a reactive() validation failures will automatically propagate to outputs that use the reactive.

validate(..., errorClass = character(0))

need(expr, message = paste(label, "must be provided"), label)

Arguments

...

A list of tests. Each test should equal NULL for success, FALSE for silent failure, or a string for failure with an error message.

errorClass

A CSS class to apply. The actual CSS string will have shiny-output-error- prepended to this value.

expr

An expression to test. The condition will pass if the expression meets the conditions spelled out in Details.

message

A message to convey to the user if the validation condition is not met. If no message is provided, one will be created using label. To fail with no message, use FALSE for the message.

label

A human-readable name for the field that may be missing. This parameter is not needed if message is provided, but must be provided otherwise.

need()

An easy way to provide arguments to validate() is to use need(), which takes an expression and a string. If the expression is not "truthy" then the string will be used as the error message.

If "truthiness" is flexible for your use case, you'll need to explicitly generate a logical values. For example, if you want allow NA but not NULL, you can !is.null(input$foo).

If you need validation logic that differs significantly from need(), you can create your own validation test functions. A passing test should return NULL. A failing test should return either a string providing the error to display to the user, or if the failure should happen silently, FALSE.

Alternatively you can use validate() within an if statement, which is particularly useful for more complex conditions:

if (input$x < 0 && input$choice == "positive") {
  validate("If choice is positive then x must be greater than 0")
}

Examples

## Only run examples in interactive R sessions
if (interactive()) {
options(device.ask.default = FALSE)

ui <- fluidPage(
  checkboxGroupInput('in1', 'Check some letters', choices = head(LETTERS)),
  selectizeInput('in2', 'Select a state', choices = c("", state.name)),
  plotOutput('plot')
)

server <- function(input, output) {
  output$plot <- renderPlot({
    validate(
      need(input$in1, 'Check at least one letter!'),
      need(input$in2 != '', 'Please choose a state.')
    )
    plot(1:10, main = paste(c(input$in1, input$in2), collapse = ', '))
  })
}

shinyApp(ui, server)

}