2

Let's say I have a function with with many arguments (e.g., plot()).

I want to add a bit of functionality to that function by creating a wrapper function around that function.

  • Random example:

    plot.new <- function() {
      windows(width = 10, height = 10)
      plot()
    }
    

My question: How do I make it so that the internal function's arguments can be provided in my new wrapper function?

  • How can I do so without retyping all the argument names from the internal function when defining my wrapper function?
theforestecologist
  • 4,667
  • 5
  • 54
  • 91

1 Answers1

2

You can use the three dots ellipsis

plot.new <- function(...) {
  windows(width = 10, height = 10)
  plot(...)
}

If you want to explicitly include any of the internal function arguments in the wrapper function list, you'll have to explicitly define that argument in the internal function as well:

plot.new <- function(x, ...) {
    graphics.off() #OPTIONAL
    windows(width = 10, height = 10)
    plot(x = x, ...)
}

#USAGE
plot.new(x = rnorm(10), y = rnorm(10), pch = 19)

And here is more discussion on using to distribute the arguments to multiple internal functions.

Community
  • 1
  • 1
d.b
  • 32,245
  • 6
  • 36
  • 77
  • Do they need to be present in both argument list locations? – theforestecologist Apr 12 '17 at 15:17
  • What happens if I "reuse" an argument name (or choose to define just one of the internal function's arguments)? For example: `plot.new <- function(x,...) { plot(...) }` – theforestecologist Apr 12 '17 at 15:21
  • Last question: can I place the ellipsis anywhere in the wrapper function argument list? Ex: `function(...,z)` vs `function(z,...)`? And if so, will that dicatate the "ordering" of the arguments if I choose to not name them when running the wrapper function? – theforestecologist Apr 12 '17 at 15:29