validate

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.

Description

For an output rendering function (e.g. renderPlot()), you may need to check that certain input values are available and valid before you can render the output. validate gives you a convenient mechanism for doing so.

Details

The validate function takes any number of (unnamed) arguments, each of which represents a condition to test. If any of the conditions represent failure, then a special type of error is signaled which stops execution. If this error is not handled by application-specific code, it is displayed to the user by Shiny.

An easy way to provide arguments to validate is to use the need function, which takes an expression and a string; if the expression is considered a failure, then the string will be used as the error message. The need function considers its expression to be a failure if it is any of the following:

  • FALSE
  • NULL
  • ""
  • An empty atomic vector
  • An atomic vector that contains only missing values
  • A logical vector that contains all FALSE or missing values
  • An object of class "try-error"
  • A value that represents an unclicked actionButton

If any of these values happen to be valid, you can explicitly turn them to logical values. For example, if you allow NA but not NULL, you can use the condition !is.null(input$foo), because !is.null(NA) == TRUE.

If you need validation logic that differs significantly from need, you can create other validation test functions. A passing test should return NULL. A failing test should return an error message as a single-element character vector, or if the failure should happen silently, FALSE.

Because validation failure is signaled as an error, you can use validate in reactive expressions, and validation failures will automatically propagate to outputs that use the reactive expression. In other words, if reactive expression a needs input$x, and two outputs use a (and thus depend indirectly on input$x), it's not necessary for the outputs to validate input$x explicitly, as long as a does validate it.

Examples

# in ui.R fluidPage( checkboxGroupInput('in1', 'Check some letters', choices = head(LETTERS)), selectizeInput('in2', 'Select a state', choices = state.name), plotOutput('plot') )
<div class="container-fluid"> <div id="in1" class="form-group shiny-input-checkboxgroup shiny-input-container"> <label class="control-label" for="in1">Check some letters</label> <div class="shiny-options-group"> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in11" value="A"/> <span>A</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in12" value="B"/> <span>B</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in13" value="C"/> <span>C</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in14" value="D"/> <span>D</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in15" value="E"/> <span>E</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="in1" id="in16" value="F"/> <span>F</span> </label> </div> </div> </div> <div class="form-group shiny-input-container"> <label class="control-label" for="in2">Select a state</label> <div> <select id="in2"><option value="Alabama" selected>Alabama</option> <option value="Alaska">Alaska</option> <option value="Arizona">Arizona</option> <option value="Arkansas">Arkansas</option> <option value="California">California</option> <option value="Colorado">Colorado</option> <option value="Connecticut">Connecticut</option> <option value="Delaware">Delaware</option> <option value="Florida">Florida</option> <option value="Georgia">Georgia</option> <option value="Hawaii">Hawaii</option> <option value="Idaho">Idaho</option> <option value="Illinois">Illinois</option> <option value="Indiana">Indiana</option> <option value="Iowa">Iowa</option> <option value="Kansas">Kansas</option> <option value="Kentucky">Kentucky</option> <option value="Louisiana">Louisiana</option> <option value="Maine">Maine</option> <option value="Maryland">Maryland</option> <option value="Massachusetts">Massachusetts</option> <option value="Michigan">Michigan</option> <option value="Minnesota">Minnesota</option> <option value="Mississippi">Mississippi</option> <option value="Missouri">Missouri</option> <option value="Montana">Montana</option> <option value="Nebraska">Nebraska</option> <option value="Nevada">Nevada</option> <option value="New Hampshire">New Hampshire</option> <option value="New Jersey">New Jersey</option> <option value="New Mexico">New Mexico</option> <option value="New York">New York</option> <option value="North Carolina">North Carolina</option> <option value="North Dakota">North Dakota</option> <option value="Ohio">Ohio</option> <option value="Oklahoma">Oklahoma</option> <option value="Oregon">Oregon</option> <option value="Pennsylvania">Pennsylvania</option> <option value="Rhode Island">Rhode Island</option> <option value="South Carolina">South Carolina</option> <option value="South Dakota">South Dakota</option> <option value="Tennessee">Tennessee</option> <option value="Texas">Texas</option> <option value="Utah">Utah</option> <option value="Vermont">Vermont</option> <option value="Virginia">Virginia</option> <option value="Washington">Washington</option> <option value="West Virginia">West Virginia</option> <option value="Wisconsin">Wisconsin</option> <option value="Wyoming">Wyoming</option></select> <script type="application/json" data-for="in2">{}</script> </div> </div> <div id="plot" class="shiny-plot-output" style="width: 100% ; height: 400px"></div> </div>
# in server.R 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 = ', ')) }) }
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 = ', ')) }) } <environment: 0x7fe829a93e00>