fork_help: Enhancements of 'ifelse'.

View source: R/fork.R

fork_helpR Documentation

Enhancements of ifelse.

Description

Return different types of objects for TRUE and FALSE and return NULL conditional on the number of TRUE values.

Usage

fork_help()

f0(x, y, n)

fork(x, y, n, na = n)

f1(x, y, n, na = n, err = n)

nll_if(x, ..., d = " ")

nll_ifs(..., .d = " ", .cond = "all")

nll_if_any(..., .d = " ")

nll_if_all(..., .d = " ")

nll_if_none(..., .d = " ")

Arguments

x

A logical scalar (if not,x it is replaced by FALSE).

y, n

Any valid R object.

na

An object of any type for f1. An atomic scalar compatible with y and n for fork, with the additional possibility of na = 'err' to indicate an error should be thrown if any values in x are NA.

err

Either 'err' or an object to be returned when x is not an atomic scalar in c(TRUE, FALSE, NA).

d, .d

A character scalar delimiter for collapsing objects into scalar character objects. If .d is not a character scalar, it is replaced by " ".

.cond

A character scalar in c('all', 'any', 'none'). If .cond is not of an allowed value, it is replaced by 'all'.

Functions

  • f0(): If x is scalar TRUE, returns y. If x is anything else, returns n.

  • fork(): Evaluates logical scalar or logical vector x and return an object of the same length as x where:

    • TRUE values of x are replaced by corresponding values of y.

    • FALSE values of x are replaced by corresponding values of n.

    • NA values of x are replaced by na (unless na = 'err', in which case if there are any NA values in x, throws an error).

    Returns a length-length(x) atomic object.

  • f1(): Error-checked version of f0. Evaluates and processes logical scalar x in the following manner:

    • If x = TRUE, returns y.

    • If x = FALSE, returns y.

    • If x = NA, returns na unless na = 'err', in which case, an error is thrown.

    • If x is neither a logical scalar nor scalar NA, returns err unless err = 'err', in which case an error is thrown.

  • nll_if(): If x is scalar TRUE, returns NULL, otherwise collapses ... args to a character scalar using delimiter d and returns the result.

  • nll_ifs(): Calls nll_if_none(..., d = .d) when .cond = 'none'. Calls nll_if_any(..., d = .d) when ⁠.cond = 'any⁠. Calls nll_if_all(..., d = .d) when .cond takes any other value (including 'all')

  • nll_if_any(): Returns NULL if any named ... argument is scalar TRUE. Otherwise, returns a message composed of unnamed ... args collapsed into a character scalar using the delimiter .d.

  • nll_if_all(): Returns NULL if all named ... arguments are scalar TRUE. Otherwise, returns a message composed of unnamed ... args collapsed into a character scalar using the delimiter .d.

  • nll_if_none(): Returns NULL if no named ... argument is scalar TRUE. Otherwise, returns a message composed of unnamed ... args collapsed into a character scalar using the delimiter .d.

See Also

Other forks: case(), swap_help()

Examples

fork(c(TRUE, FALSE, TRUE, NA), 1, 2)
fork(c(TRUE, FALSE, TRUE, NA), 1, 2, na = 0)
fork(c(TRUE, FALSE, TRUE, NA), 1, 2, na = NA)
fork(c(TRUE, FALSE, TRUE, NA), 1:4, 5:8)

f0(NA, data.frame(letters = letters), 0:26)
f0(TRUE, data.frame(letters = letters), 0:26)
f0(FALSE, data.frame(letters = letters), 0:26)
f0(list(1, "a"), data.frame(letters = letters), 0:26)
f0(c(.bad.varname.), data.frame(letters = letters), 0:26)

f1(NA, data.frame(letters = letters), 0:26)
f1(NA, data.frame(letters = letters), 0:26, na = NA)
f1(TRUE, data.frame(letters = letters), 0:26)
f1(FALSE, data.frame(letters = letters), 0:26)
f1(list(1, "a"), data.frame(letters = letters), 0:26)
f1(list(1, "a"), data.frame(letters = letters), 0:26)
f1(c(.bad.varname.), data.frame(letters = letters), 0:26)
f1(list(1, "a"), data.frame(letters = letters), 0:26, err = "error")
f1(c(.bad.varname.), data.frame(letters = letters), 0:26, err = "error")

nll_if(TRUE, "an error", "message")
nll_if(FALSE, "an error", "message")
nll_if(41, "an error", "message")

nll_ifs(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "any", .cond = "all")
nll_ifs(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "all", .cond = "any")
nll_ifs(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "none", .cond = "none")
nll_if_any(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "any")
nll_if_all(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "all")
nll_if_none(t1 = TRUE, t2 = FALSE, t3 = FALSE, "not", "none")

j-martineau/uj documentation built on Sept. 14, 2024, 4:40 a.m.