2

I want to have a function with optional arguments, something like

retVal <- myFunction(mandetory_arguments, ...) {
            // DO something with ...'s
}

I am new to R and know that optional/additional arguments can be passed using ..., but have no clue how it is used for further parsing. Is there a way for a function to have optional arguments?

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Nikhil J Joshi
  • 1,177
  • 2
  • 12
  • 25
  • 5
    Here's a guide: http://stackoverflow.com/questions/3057341/how-to-use-rs-ellipsis-feature-when-writing-your-own-function – lukeA Feb 24 '14 at 11:27

1 Answers1

2

Here's a simple example of a customized plot function that can pass arguments to plot by using ...:

funky.plot <- function(x,y,...){
  op <- par(bg=1, mar=c(1,1,4,4))
  plot(x,y, pch=21, col=4, bg="yellow", lwd=2, cex=2, col.main="yellow", ...)
  axis(3, col="yellow", lwd=2, col.axis="yellow")
  axis(4, col="yellow", lwd=2, col.axis="yellow")
  box(col="pink", lwd=3)
  par(op)
}

Here, the additional argument of a title (i.e main=) is possible because of the ... addition:

x <- runif(100)
y <- 2*x+rnorm(100)

funky.plot(x,y, main="wow")

enter image description here

Marc in the box
  • 11,769
  • 4
  • 47
  • 97