I am trying to do something very similar to here.
Essentially, I need to pass a named list to a function, and give that named list to another function as its parameters.
If we follow that link, they are able to do it with mutate, and I can replicate that:
df <- tribble(
~a,
1
)
foo <- function(x, args) {
mutate(x, !!! args)
}
foo(df, quos(b = 2, c = 3)
# A tibble: 1 x 3
a b c
<dbl> <dbl> <dbl>
1 1 2 3
But if I try to do it with any other function, it fails. Say, if I try to use print, (which the first parameter is x, so I pass a named list with x in it):
print(x= "hello")
[1] "hello"
foo <- function(x, args) {
print(!!! args)
}
foo(df, quos(x = "hello"))
Error in !args : invalid argument type
I'm not sure why this won't work outside of the "tidyverse" functions. I've tried different combinations of sym, enquo, bang bang, curly curly, etc., but to no avail.
Of course my final goal is not to use print but to use another user-defined-function in its place, so if you have any advice on how to achieve that, I would also greatly appreciate it. (And by the way, I do have to use a named list, I don't think I can use ...).
Thank you so much for your help.