I am developing an R package in which I have an exported function that needs to call several unexported functions and store the results from each in a list. Which functions are called is variable and depends on user input.
My approach to this was to lapply a (character) vector of function names with do.call, but it seems like this makes the unexported functions invisible to the exported function.
Consider the following example package code:
tmp1 <- function(x) print(paste("Function 1 called with x =", x))
tmp2 <- function(x) print(paste("Function 2 called with x =", x))
tmp3 <- function(x) print(paste("Function 3 called with x =", x))
#' @export
test1 <- function() {
  tmp1("test")
  tmp2("test")
  tmp3("test")
}
#' @export
test2 <- function() {
  funs <- c("tmp1", "tmp2", "tmp3")
  for (fun in funs) do.call(fun, list(x = "test"))
}
#' @export
test3 <- function() {
  funs <- c("tmp1", "tmp2", "tmp3")
  lapply(funs, do.call, list(x = "test"))
}
After building and loading the package, running the three test functions yield the following output:
test1()
#> [1] "Function 1 called with x = test"
#> [1] "Function 2 called with x = test"
#> [1] "Function 3 called with x = test"
test2()
#> [1] "Function 1 called with x = test"
#> [1] "Function 2 called with x = test"
#> [1] "Function 3 called with x = test"
test3()
#> Error in tmp1(x = "test"): could not find function "tmp1"
Calling the functions directly works, and calling them with do.call works when using do.call directly, but it fails when calling them via lapply.
I can make a workaround with the for-loop, but I am curious as to why this happens.
So, my question is twofold:
- Why are the unexported functions invisible to do.callwhen called insidelapply?
- Can I make the lapply(funs, do.call, list(...))approach work?
 
    