The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

If

Colin Fay

2020-05-03

if_ conditions

if_none(), if_any() and if_all() test the elements of the list.

if_all(1:10, ~ .x < 11, ~ return(letters[1:10]))
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

if_any(1:10, is.numeric, ~ "Yay!")
#> [1] "Yay!"

if_none(1:10, is.character, ~ rnorm(10))
#>  [1] -0.01796899 -0.61090476 -0.47344389  0.41872841  0.31989097  1.64960057
#>  [7] -0.04011638 -1.55776569  1.45658440 -0.53125705

The defaut for all .p is isTRUE(). So you can:

a <- c(FALSE, TRUE, TRUE, TRUE)

if_any(a, .f = ~ "nop!")
#> [1] "nop!"

if_then() performs a simple “if this then do that”:

if_then(1, is.numeric, ~ "nop!")
#> [1] "nop!"

if_not() runs .f if .p(.x) is not TRUE :

if_not(.x = 1, .p = is.character, ~ ".x is not a character")
#> [1] ".x is not a character"

And if_else() is a wrapper around base::ifelse().

If you want these function to return a value, you need to wrap these values into a mapper / a function. E.g, to return a vector, you’ll need to write if_then(1, is.numeric, ~ "Yay").

a <- if_else(1, is.numeric, ~ "Yay", ~ "Nay")
a
#> [1] "Yay"

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.