I'm running a function with many arguments and I'm exploring how changes in some arguments affect the output of the function. I'm doing that through purrr::pmap
. I'd like to keep track of the arguments used for each function call: I'd like the function to return its output, as well as a named list of the values of all the arguments used.
Here's a 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)
I'd like effect_of_a
to be a list of list, where each sublist contains not only the result f(a,b,c)
, but also the values of a, b and c used. I could code that list manually, but I have many arguments and they may change. So is there a way to capture the list of arguments and their values in a function call initiated by purrr:pmap
?
Here is a general solution where the arguments are captured in an extra function
f_2
, which is called withpmap
instead off
: