I am trying to produce a list of graphical objects (grobs), suitable for passing to gridExtra::grid.arrange (or some ggequivalent if that is easier). I have redrafted my example to eliminate multiple problems that were kindly pointed out by @MrFlick and @Ronak Shah. However, I still get the same error message:
Error in .f(.x[[i]], ...) : unused argument (NULL)
(and I don't get a single google hit for "unused argument (NULL)
" r
| purrr
. The closest I find is unused argument (.null = NA)
.)
Each object is a set of four plots (plotted together) that are output as side effects from the acf
or pacf
functions, showing the autocorelation and autocovarience between two variables and their various lags. I am trying to write a function that will take a tsibble, a vector of independent variable column names from that tsibble, and the name of a single outcome variable (also a column in the input tsibble) and, using purrr, take the acf
or pacf
of the pairs of variables defined by the single name with each of the specified columns, convert the plot to a grob, and output a list of the grobs.
Here is my non-working function, acf version:
acf_grobs <- function(dat., mod_nms, outcome){
depend <- select(dat., all_of(mod_nms))
out <- map(depend, ~ grob(acf(x=cbind(.x, y))), y = dat.[[outcome]])
out
}
And a reproducible example:
library(tidyverse)
library(fpp3)
I know that this loads a lot more packages than needed for this example, but I am loading all these packages for other reasons.
aa <- 1:4
bb <- 1:4
cc <- 1:4
day <- 1:4
tb <- tibble(aa, bb, cc, day)
tsb <- as_tsibble(tb, index = day)
var_of_interest <- "aa"
mod.nms <- c("bb", "cc")
pacf_grobs(dat. = tsb, mod_nms = mod.nms, outcome = var_of_interest)
I still believe the error message is caused by my failure to pass the single argument outcome
to the function argument of purrr::map correctly, though this belief is shaken because I am now passing it a different way, and still getting the same error message.