0

For example, can I do something like this?

f1 <- function(x, y){
  x + y
}
f2 <- function(a, b){
  a * b
}
ff <- function(x, a, ...){
  cat(f1(x, ...))
  cat(f2(a, ...))
}

I have tried the above but it got an error Error in f1(x, ...) : unused argument (1).

Zhiwei
  • 1
  • 1
  • 2
    `...` works well for passing one or many argument to a subfunction - or multiple subfunctions, but only if you want to pass **the same** `...` arguments to all of them. With your code, `ff(1, 2, 3)` does fine with `... = 3` for both functions. If you want to pass different arguments to different subfunctions you will need a more sophisticated approach, perhaps using lists for each subfunction and calling them with `do.call`, perhaps just using all named arguments, or perhaps another solution - it can depend on the use case and how complicated the dispatch needs to be. – Gregor Thomas Mar 06 '23 at 16:16
  • **How** have you tried the above? When I try it it works. – Konrad Rudolph Mar 06 '23 at 16:24
  • Hi @KonradRudolph! I tried as follows `ff(x = 1, a = 1, y = 3, b = 10)` – Zhiwei Mar 09 '23 at 13:15
  • So, you want to pass different parameters to different subfunctions. Separate lists is probably the best way. If you look at the accepted answer at the marked duplicate it walks through examples. Note the first sentence is **"Separate Lists If you really want to pass different sets of parameters to different functions then it's probably cleaner to specify separate lists:"** – Gregor Thomas Mar 09 '23 at 13:49
  • @GregorThomas Hi Gregor! Thank you for the reply! I tried do.call() with separated lists and it works! – Zhiwei Mar 10 '23 at 14:06

0 Answers0