lift_xy() is a composition helper. It helps you compose functions by lifting their domain from a kind of input to another kind. The domain can be changed from and to a list (l), a vector (v) and dots (d). For example, lift_ld(fun) transforms a function taking a list to a function taking dots.

lift(..f, ..., .unnamed = FALSE)

lift_dl(..f, ..., .unnamed = FALSE)

lift_dv(..f, ..., .unnamed = FALSE)

lift_vl(..f, ..., .type)

lift_vd(..f, ..., .type)

lift_ld(..f, ...)

lift_lv(..f, ...)

Arguments

..f

A function to lift.

...

Default arguments for ..f. These will be evaluated only once, when the lifting factory is called.

.unnamed

If TRUE, ld or lv will not name the parameters in the lifted function signature. This prevents matching of arguments by name and match by position instead.

.type

A vector mold or a string describing the type of the input vectors. The latter can be any of the types returned by typeof(), or "numeric" as a shorthand for either "double" or "integer".

Value

A function.

Details

The most important of those helpers is probably lift_dl() because it allows you to transform a regular function to one that takes a list. This is often essential for composition with purrr functional tools. Since this is such a common function, lift() is provided as an alias for that operation.

from ... to list(...) or c(...)

Here dots should be taken here in a figurative way. The lifted functions does not need to take dots per se. The function is simply wrapped a function in do.call(), so instead of taking multiple arguments, it takes a single named list or vector which will be interpreted as its arguments. This is particularly useful when you want to pass a row of a data frame or a list to a function and don't want to manually pull it apart in your function.

from c(...) to list(...) or ...

These factories allow a function taking a vector to take a list or dots instead. The lifted function internally transforms its inputs back to an atomic vector. purrr does not obey the usual R casting rules (e.g., c(1, "2") produces a character vector) and will produce an error if the types are not compatible. Additionally, you can enforce a particular vector type by supplying .type.

from list(...) to c(...) or ...

lift_ld() turns a function that takes a list into a function that takes dots. lift_vd() does the same with a function that takes an atomic vector. These factory functions are the inverse operations of lift_dl() and lift_dv().

lift_vd() internally coerces the inputs of ..f to an atomic vector. The details of this coercion can be controlled with .type.

See also

Examples