[R] Capturing Function Arguments

Reed A. Cartwright r@c@rtwr|ght @end|ng |rom gm@||@com
Sat Feb 17 19:15:43 CET 2024


I'm wrapping a function in R and I want to record all the arguments
passed to it, including default values and missing values. I want to
be able to snoop on function calls in sourced scripts as part of a
unit testing framework.

I can capture the values fine, but I'm having trouble evaluating them
as if `force()` had been applied to each of them.

Here is a minimal example:

f0 <- function(x, y = 2 * z, z, a = NULL, b) NULL

f <- function(...) {
  call <- rlang::call_match(fn = f0, defaults = TRUE)
  args <- rlang::call_args(call)
  # do something here to evaluate args as if force() had been called
  # I've tried many things but haven't found a solution that handled everything
  args
}

# In the below example args1 and args2 should be the same
a <- 1
args1 <- f(a, z = 1 + 100)

args2 <- list( a = 1, y = 202, z = 101, a = NULL, b = rlang::missing_arg() )

If anyone knows how to get this to work, I would appreciate the help.

Thanks,
Reed

--
Reed A. Cartwright, PhD
Associate Professor of Genomics, Evolution, and Bioinformatics
School of Life Sciences and The Biodesign Institute
Arizona State University
==================
Address: The Biodesign Institute, PO Box 876401, Tempe, AZ 85287-6401 USA
Packages: The Biodesign Institute, 1001 S. McAllister Ave, Tempe, AZ
85287-6401 USA
Office: Biodesign B-220C, 1-480-965-9949
Website: http://cartwrig.ht/



More information about the R-help mailing list