Search code examples
rellipsisnested-function

How do I save the contents of ellipsis in R to be used in deeply nested function calls?


I'm trying to write a function that iterates through all the files in the immediate sub-directories of a directory, and then runs a custom-written function to extract features from the text of the files:

load.samples <- function(path = ".", ...) {
  dirs <- dir(path, full.names = TRUE)[file.info(dir(path, full.names = TRUE))$isdir]
  lapply(dirs, function(p) {
    lapply(dir(p, full.names = TRUE), function(f) {
      text <- readChar(f, file.info(f)$size)
      extractFeatures(text, ...) #I want this call to pass along the extra parameters to load.samples
    })
  })
}

I'm trying to have any additional arguments passed to the load.samples function be carried through to the extractFeatures function. However, the code above obviously doesn't work, because instead of being directly called, the call to extractFeatures is wrapped inside two nested functions that are called via two nested calls to lapply, and I get an unused argument error whenever I try to pass any extra parameters to load.samples.

What is the simplest way to save the value of the ellipses to be used in such nested functions?


Solution

  • Change the first line to this:

    load.samples <- function(..., path = ".") {
    

    The trick is that ... should come after the regular parameters, but before the named parameters.