req(...)
req(input$a != 0)
req
function was designed to be used in one of two ways. The first
is to call it like a statement (ignoring its return value) before attempting
operations using the required values:rv <- reactiveValues(state = FALSE) r <- reactive({ req(input$a, input$b, rv$state) # Code that uses input$a, input$b, and/or rv$state... })
In this example, if r()
is called and any of input$a
,
input$b
, and rv$state
are NULL
, FALSE
, ""
,
etc., then the req
call will trigger an error that propagates all the
way up to whatever render block or observer is executing.
The second is to use it to wrap an expression that must be truthy:
output$plot <- renderPlot({ if (req(input$plotType) == "histogram") { hist(dataset()) } else if (input$plotType == "scatter") { qplot(dataset(), aes(x = x, y = y)) } })
In this example, req(input$plotType)
first checks that
input$plotType
is truthy, and if so, returns it. This is a convenient
way to check for a value "inline" with its first use.
Truthy and falsy values
The terms "truthy" and "falsy" generally indicate whether a value, when
coerced to a logical
, is TRUE
or FALSE
. We use
the term a little loosely here; our usage tries to match the intuitive
notions of "Is this value missing or available?", or "Has the user provided
an answer?", or in the case of action buttons, "Has the button been
clicked?".
For example, a textInput
that has not been filled out by the user has
a value of ""
, so that is considered a falsy value.
To be precise, req
considers a value truthy unless it is one
of:
FALSE
NULL
""
FALSE
or missing values"try-error"
actionButton