I have a “master” function foo
from which I need to call two other functions fun1
and fun2
. Each of these has separate argument sets which I'd like to set from outside.
library(tidyverse)
#> Registered S3 methods overwritten by 'ggplot2':
#> method from
#> [.quosures rlang
#> c.quosures rlang
#> print.quosures rlang
fun1 <- function(a, c) {
print(glue::glue("a is {a}"))
print(glue::glue("c is {c}"))
}
fun2 <- function(e, g) {
print(glue::glue("e is {e}"))
print(glue::glue("g is {g}"))
}
foo <- function(something, additional_args) {
print(glue::glue("Called with {something}"))
# I know I can do this, but I don't want to explicitly specify a, c, e and g here!)
fun1(additional_args$fun1_args$a, additional_args$fun1_args$c)
fun2(additional_args$fun2_args$e, additional_args$fun2_args$g)
}
foo("something", additional_args = list(
fun1_args = list(a = "b", c = "d"),
fun2_args = list(e = "f", g = "h")
))
#> Called with something
#> a is b
#> b is d
#> c is f
#> d is h
Created on 2019-07-17 by the reprex package (v0.3.0)
Obviously, it's not very nice that I have to repeat the names of the arguments inside foo
. And the functions fun1
and fun2
may take many more arguments, or not all of them, or have some defaults.
I don't think I've fully grasped quasiquotation. And I read through the documentation, but nothing there applies. I am not using ...
anywhere since I don't want to mix up arguments for function fun1
and fun2
.
How can I replace these weird calls, in which I explicitly specify the arguments, so that they just forward the arguments instead?
fun1(additional_args$fun1_args$a, additional_args$fun1_args$c)
fun2(additional_args$fun2_args$e, additional_args$fun2_args$g)
I have tried
fun1_args = enquos(additional_args$fun1_args)
fun1(!!! fun1_args)
But it says:
Error: Inputs to capture must be argument names
I've also tried fun1(!!!additional_args$fun1_args)
, but it says:
Error in !additional_args$fun1_args : invalid argument type