Debian Codegraph @

man/req.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/utils.R
3 \name{req}
4 \alias{req}
5 \title{Check for required values}
6 \usage{
7 req(..., cancelOutput = FALSE)
8 }
9 \arguments{
10 \item{...}{Values to check for truthiness.}
11
12 \item{cancelOutput}{If \code{TRUE} and an output is being evaluated, stop
13 processing as usual but instead of clearing the output, leave it in
14 whatever state it happens to be in. If \code{"progress"}, do the same as \code{TRUE},
15 but also keep the output in recalculating state; this is intended for cases
16 when an in-progress calculation will not be completed in this reactive
17 flush cycle, but is still expected to provide a result in the future.}
18 }
19 \value{
20 The first value that was passed in.
21 }
22 \description{
23 Ensure that values are available (\link[=isTruthy]{"truthy"}) before proceeding
24 with a calculation or action. If any of the given values is not truthy, the
25 operation is stopped by raising a "silent" exception (not logged by Shiny,
26 nor displayed in the Shiny app's UI).
27 }
28 \details{
29 The \code{req} function was designed to be used in one of two ways. The first
30 is to call it like a statement (ignoring its return value) before attempting
31 operations using the required values:
32
33 \if{html}{\out{<div class="sourceCode">}}\preformatted{rv <- reactiveValues(state = FALSE)
34 r <- reactive(\{
35 req(input$a, input$b, rv$state)
36 # Code that uses input$a, input$b, and/or rv$state...
37 \})
38 }\if{html}{\out{</div>}}
39
40 In this example, if \code{r()} is called and any of \code{input$a},
41 \code{input$b}, and \code{rv$state} are \code{NULL}, \code{FALSE}, \code{""},
42 etc., then the \code{req} call will trigger an error that propagates all the
43 way up to whatever render block or observer is executing.
44
45 The second is to use it to wrap an expression that must be truthy:
46
47 \if{html}{\out{<div class="sourceCode">}}\preformatted{output$plot <- renderPlot(\{
48 if (req(input$plotType) == "histogram") \{
49 hist(dataset())
50 \} else if (input$plotType == "scatter") \{
51 qplot(dataset(), aes(x = x, y = y))
52 \}
53 \})
54 }\if{html}{\out{</div>}}
55
56 In this example, \code{req(input$plotType)} first checks that
57 \code{input$plotType} is truthy, and if so, returns it. This is a convenient
58 way to check for a value "inline" with its first use.
59 }
60 \section{Using \code{req(FALSE)}}{
61
62
63 You can use \code{req(FALSE)} (i.e. no condition) if you've already performed
64 all the checks you needed to by that point and just want to stop the reactive
65 chain now. There is no advantage to this, except perhaps ease of readability
66 if you have a complicated condition to check for (or perhaps if you'd like to
67 divide your condition into nested \code{if} statements).
68 }
69
70 \section{Using \code{cancelOutput = TRUE}}{
71
72
73 When \code{req(..., cancelOutput = TRUE)} is used, the "silent" exception is
74 also raised, but it is treated slightly differently if one or more outputs are
75 currently being evaluated. In those cases, the reactive chain does not proceed
76 or update, but the output(s) are left is whatever state they happen to be in
77 (whatever was their last valid state).
78
79 Note that this is always going to be the case if
80 this is used inside an output context (e.g. \code{output$txt <- ...}). It may
81 or may not be the case if it is used inside a non-output context (e.g.
82 \code{\link[=reactive]{reactive()}}, \code{\link[=observe]{observe()}} or \code{\link[=observeEvent]{observeEvent()}})
83 --- depending on whether or not there is an \code{output$...} that is triggered
84 as a result of those calls. See the examples below for concrete scenarios.
85 }
86
87 \examples{
88 ## Only run examples in interactive R sessions
89 if (interactive()) {
90 ui <- fluidPage(
91 textInput('data', 'Enter a dataset from the "datasets" package', 'cars'),
92 p('(E.g. "cars", "mtcars", "pressure", "faithful")'), hr(),
93 tableOutput('tbl')
94 )
95
96 server <- function(input, output) {
97 output$tbl <- renderTable({
98
99 ## to require that the user types something, use: `req(input$data)`
100 ## but better: require that input$data is valid and leave the last
101 ## valid table up
102 req(exists(input$data, "package:datasets", inherits = FALSE),
103 cancelOutput = TRUE)
104
105 head(get(input$data, "package:datasets", inherits = FALSE))
106 })
107 }
108
109 shinyApp(ui, server)
110 }
111 }