VectorizeM {wrapr} | R Documentation |
Memoizing wrapper to base::Vectorize()
Description
Build a wrapped function that applies to each unique argument in a vector of arguments once.
Usage
VectorizeM(
FUN,
vectorize.args = arg.names,
SIMPLIFY = TRUE,
USE.NAMES = TRUE,
UNLIST = FALSE
)
Arguments
FUN |
function to apply |
vectorize.args |
a character vector of arguments which should be vectorized. Defaults to first argument of FUN. If set must be length 1. |
SIMPLIFY |
logical or character string; attempt to reduce the result to a vector, matrix or higher dimensional array; see the simplify argument of sapply. |
USE.NAMES |
logical; use names if the first ... argument has names, or if it is a character vector, use that character vector as the names. |
UNLIST |
logical; if TRUE try to unlist the result. |
Details
Only sensible for pure side-effect free deterministic functions.
Value
adapted function (vectorized with one call per different value).
See Also
Examples
fs <- function(x) { x <- x[[1]]; print(paste("see", x)); sin(x) }
fv <- VectorizeM(fs)
# should only print "see" twice, not 6 times
fv(c(0, 1, 1, 0, 0, 1))
[Package wrapr version 2.1.0 Index]