获取提供给 purrr::pmap 调用的参数

Ben*_*Ben 1 arguments r pmap purrr

我正在运行一个带有许多参数的函数,我正在探索某些参数的变化如何影响函数的输出。我正在通过purrr::pmap. 我想跟踪用于每个函数调用的参数:我希望函数返回其输出,以及所有使用的参数值的命名列表。

这是一个 MWE:

f <- function (a, b, c) a + b + c
a_values <- 1:5
effect_of_a <- pmap(list(a = a_values), f, b = 0, c = 0)
Run Code Online (Sandbox Code Playgroud)

我想effect_of_a成为一个列表列表,其中每个子列表不仅包含结果f(a,b,c),还包含使用的 a、b 和 c 的值。我可以手动编码该列表,但我有很多参数,它们可能会改变。那么有没有办法在由 发起的函数调用中捕获参数列表及其值purrr:pmap

Wal*_*ldi 5

你可以使用with

pmap(list(a = a_values), ~with(list(...), list(f = f(...),args = list(...))),
          b = 0,
          c = 0)

[[1]]
[[1]]$f
[1] 1

[[1]]$args
[[1]]$args$a
[1] 1

[[1]]$args$b
[1] 0

[[1]]$args$c
[1] 0
...
Run Code Online (Sandbox Code Playgroud)