Invalid inputs
- Condition must evaluate to a single
TRUE
orFALSE
A single number gets coerced to a logical type.
If the condition cannot evaluate to a single TRUE
or FALSE
, an error is (usually) produced.
if ("text") 1
#> Error in if ("text") 1: argument is not interpretable as logical
if ("true") 1
#> 1
if (numeric()) 1
#> Error in if (numeric()) 1: argument is of length zero
if (NULL) 1
#> Error in if (NULL) 1 : argument is of length zero
if (NA) 1
#> Error in if (NA) 1: missing value where TRUE/FALSE needed
Exception is a logical vector of length greater than 1, which only generates a warning, unless you have _R_CHECK_LENGTH_1_CONDITION_
set to TRUE
.
This seems to have been the default since R-4.2.0