zplapply: Apply a Function to Multiple List or Vector Arguments

.plapplyR Documentation

Apply a Function to Multiple List or Vector Arguments

Description

Alternate versions of plapply, psapply, and pvapply which accept an argument dots instead of ....

Usage

.plapply(X, FUN, dots = NULL)

.psapply(X, FUN, dots = NULL, simplify = TRUE, USE.NAMES = TRUE)

.pvapply(X, FUN, FUN.VALUE, dots = NULL, USE.NAMES = TRUE)

Arguments

X

list of arguments to vectorize over.

FUN

function to apply, found via match.fun.

dots

list of optional arguments to FUN.

simplify

logical or character string; should the result be simplified to a vector, matrix, or higher dimensional array if possible? The default value, TRUE, returns a vector or matrix if appropriate, whereas if simplify = "array" the result may be an array of higher dimension.

USE.NAMES

logical; if TRUE and one of X is character and of equal length to the result, use that element of X as names for the result unless it had names already.

FUN.VALUE

a (generalized) vector; a template for the return value from FUN.

Details

While plapply would build a call like:

FUN(X[[1L]][[j]], X[[2L]][[j]], ...)

.plapply builds a call like:

FUN(X[[1L]][[j]], X[[2L]][[j]], dots[[1L]], dots[[2L]])

with more or less dots[[k]] depending on the length of dots. Additionally, the dots arguments in the call will be named if dots has names.

dots is not coerced by base::as.list, instead its subsetting ([[), length, and names methods will be used.

Note

plapply will only evaluate its ... argument as necessary, and even when forced, it will only evaluate the desired elements.

In contrast, .plapply will ALWAYS evaluate its dots argument, even if it is never used. This is because we need to know dots length and names to build the call.

See Also

plapply

Examples

# you should see here that plapply will not evaluate its
# optional arguments to FUN (because they are not used in
# this example)
#
# but .plapply will evaluate its optional arguments, even
# though they are not used in this example
invisible(essentials:: plapply(NA, function(...) {
    print(substitute(list(...)))
},      k = cat("evaluated optional arguments to FUN\n") ))
invisible(essentials::.plapply(NA, function(...) {
    print(substitute(list(...)))
}, list(k = cat("evaluated optional arguments to FUN\n"))))


# also, plapply will only evaluate optional arguments as
# requested
invisible(essentials:: plapply(NA, function(x, ...) ..1,
         cat("evaluated first optional argument to FUN\n"),
         cat("evaluated second optional argument to FUN\n") ))
invisible(essentials::.plapply(NA, function(x, ...) ..1,
    list(cat("evaluated first optional argument to FUN\n"),
         cat("evaluated second optional argument to FUN\n"))))

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.