I have two functions nested in another function and I would like that the arguments declared in parent were passed to the child (only when the argument is relevant to the function).
# child function 1 child_f1 <- function(x1 = 1, x2 = 3) { res <- x1^2 + 4 * x2 } # child function 2 child_f2 <- function(z = 2) { res <- z * 1.345 } # parent function parent_f <- function(y = 4, ...) { res <- (child_f1(...) ^ y) + child_f2(...) # print(res) return(res) } Test below:
parent_f(y = 2, x1 = 2, x2 = 0, z = 3) # Error in child_f1(...) (from #2) : unused argument (z = 3) # Expected result: (((2)^2 + 4*(0)) ^ (2)) + (3) * 1.345 [1] 20.04 How do I tell child_f1 that has to use only x1 and x2 (if available, otherwise use default value) and child_f2 that has to use only z(if available, otherwise use default value)?
I would like to stick with the usage of ... rather than re-write a parent_f() with all the possible parameters declared.