Debian Codegraph @

man/validate.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/utils.R
3 \name{validate}
4 \alias{validate}
5 \alias{need}
6 \title{Validate input values and other conditions}
7 \usage{
8 validate(..., errorClass = character(0))
9
10 need(expr, message = paste(label, "must be provided"), label)
11 }
12 \arguments{
13 \item{...}{A list of tests. Each test should equal \code{NULL} for success,
14 \code{FALSE} for silent failure, or a string for failure with an error
15 message.}
16
17 \item{errorClass}{A CSS class to apply. The actual CSS string will have
18 \verb{shiny-output-error-} prepended to this value.}
19
20 \item{expr}{An expression to test. The condition will pass if the expression
21 meets the conditions spelled out in Details.}
22
23 \item{message}{A message to convey to the user if the validation condition is
24 not met. If no message is provided, one will be created using \code{label}.
25 To fail with no message, use \code{FALSE} for the message.}
26
27 \item{label}{A human-readable name for the field that may be missing. This
28 parameter is not needed if \code{message} is provided, but must be provided
29 otherwise.}
30 }
31 \description{
32 \code{validate()} provides convenient mechanism for validating that an output
33 has all the inputs necessary for successful rendering. It takes any number
34 of (unnamed) arguments, each representing a condition to test. If any
35 of condition fails (i.e. is not \link[=isTruthy]{"truthy"}), a special type of
36 error is signaled to stop execution. If this error is not handled by
37 application-specific code, it is displayed to the user by Shiny.
38
39 If you use \code{validate()} in a \code{\link[=reactive]{reactive()}} validation failures will
40 automatically propagate to outputs that use the reactive.
41 }
42 \section{\code{need()}}{
43
44 An easy way to provide arguments to \code{validate()} is to use \code{need()}, which
45 takes an expression and a string. If the expression is not
46 \link[=isTruthy]{"truthy"} then the string will be used as the error message.
47
48 If "truthiness" is flexible for your use case, you'll need to explicitly
49 generate a logical values. For example, if you want allow \code{NA} but not
50 \code{NULL}, you can \code{!is.null(input$foo)}.
51
52 If you need validation logic that differs significantly from \code{need()}, you
53 can create your own validation test functions. A passing test should return
54 \code{NULL}. A failing test should return either a string providing the error
55 to display to the user, or if the failure should happen silently, \code{FALSE}.
56
57 Alternatively you can use \code{validate()} within an \code{if} statement, which is
58 particularly useful for more complex conditions:
59
60 \if{html}{\out{<div class="sourceCode">}}\preformatted{if (input$x < 0 && input$choice == "positive") \{
61 validate("If choice is positive then x must be greater than 0")
62 \}
63 }\if{html}{\out{</div>}}
64 }
65
66 \examples{
67 ## Only run examples in interactive R sessions
68 if (interactive()) {
69 options(device.ask.default = FALSE)
70
71 ui <- fluidPage(
72 checkboxGroupInput('in1', 'Check some letters', choices = head(LETTERS)),
73 selectizeInput('in2', 'Select a state', choices = c("", state.name)),
74 plotOutput('plot')
75 )
76
77 server <- function(input, output) {
78 output$plot <- renderPlot({
79 validate(
80 need(input$in1, 'Check at least one letter!'),
81 need(input$in2 != '', 'Please choose a state.')
82 )
83 plot(1:10, main = paste(c(input$in1, input$in2), collapse = ', '))
84 })
85 }
86
87 shinyApp(ui, server)
88
89 }
90 }