with_args: Run FUN and capture args

View source: R/with_args.R

with_argsR Documentation

Run FUN and capture args

Description

Run a function and capture parameters

Usage

with_args(fun_name, ...)

Arguments

fun_name

input FUN. Can be bare or quoted vecor of length 1.

...

args for fun_name, preferably named

Details

This function simply captures input args as a list, useful mainly e.g. to pass args downstream. It is best practice to name funargs for this use case, both to ensure integrity and to facilitate downstream (re)use. Not using explicit names for input FUN can work, i.e. positional matching, in some cases, but more often than not will lead to problems. See examples.

Value

A list of length 3, where the first element is the input fun_name (chr), the second is the function return, and the third the args. If funargs are input as a named list (vector), the args will be named.

The length of each top-level return element depends on the input fun and args.

Note

Error handling for the edge case where a function arg is called 'fun_name' and passed to ... is not yet present. Also see examples.

Examples

with_args(mean, x = 1:10, na.rm = TRUE)
with_args("mean", x = 1:10, na.rm = TRUE) #identical
with_args(mean, 1:10) #positional matching, dangerous!

## Not run: 
problemFUN <- function(arg1, fun_name) {
arg1 + fun_name
}
with_args(problemFUN, arg1 = 1, fun_name = 2) #error
with_args(problemFUN, arg1 = 1, 2) #no error, but ugly

with_args(mean, 1:10, TRUE) #error, don't rely on positional matching!

## End(Not run)

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.