In answer to the first question, Yes, you can use multiple functions, but second and subsequent functions need to be passed on to first function and then on to next function etc. Hence the functions need to be coded so as to take additional arguments and pass them on.
For example
foo <- function(x, f1, ...) f1(x, ...)
bar <- function(y, f2, ...) f2(y, ...)
foobar <- function(z, f3, ...) f3(z)
sapply(1:10, foo, f1 = bar, y = 2, f2 = foobar, z = 4, f3 = seq_len)
> sapply(1:10, foo, f1 = bar, y = 2, f2 = foobar, z = 4, f3 = seq_len)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 1 1 1 1 1 1 1
[2,] 2 2 2 2 2 2 2 2 2 2
[3,] 3 3 3 3 3 3 3 3 3 3
[4,] 4 4 4 4 4 4 4 4 4 4
This is a silly example but it shows how to pass on extra arguments to foo()
, initially, as part of the ...
argument of sapply()
. Also it shows how to have foo()
and subsequent functions take extra arguments to be passed on, simply via the use of ...
in the function definition and in how the next function is called, e.g. f2(y, ...)
. Note I also avoid issue with positional matching and name all the additional arguments supplied to foo()
.
With regard to question 2, I think the way you explain it is over-complicating things. You have, for example, duplicated the reg.data
and reg.fcn
bits in what R iterates over with sapply()
, which isn't correct (it implies you iterate over the 3 things in the vector c(1:n,reg.data,reg.fcn)
, not over 1:n
).
sapply(1:n, fun, arg1, arg2)
is equivalent to
fun(1, arg1, arg2)
fun(2, arg1, arg2)
....
fun(10, arg1, arg2)
whilst sapply(1:n, fun, arg1 = bar, arg2 = foobar)
is equivalent to
fun(1, arg1 = bar, arg2 = foobar)
fun(2, arg1 = bar, arg2 = foobar)
....
fun(10, arg1 = bar, arg2 = foobar)