Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple three dot ellipsis in R [duplicate]

Tags:

r

Is there a way to pass arbitrary arguments to more than one command inside a function? The following function clearly does not work but I hope it explains what I am trying to achieve.

test = function(x = rnorm(20), y = rnorm(20), ..., ---){
    plot(x, y, type = "p", ...)
    lines(x, y, ---) 
}

The goal is to be able to write a function that creates plot with say lines and points and polygon and can take arbitrary arguments for each command and pass them to the respective commands without me having to explicitly specify arguments for each command.

like image 920
d.b Avatar asked Feb 05 '23 04:02

d.b


1 Answers

Here is a hackish approach:

.. <- "/////" #or anything which won't be used as a valid parameter

f <- function(...){
  arguments <- list(...)
  if(.. %in% arguments){
    i <- which(arguments == ..)
    terms <- unlist(arguments[1:(i-1)])
    factors <- unlist(arguments[(i+1):length(arguments)])
    c(sum(terms),prod(factors))
  }
}

Then, for example,

> f(2,3,4,..,7,8,10)
[1]   9 560

You could obviously extend the idea to multiple ... fields, each delimited with ..

like image 197
John Coleman Avatar answered Feb 20 '23 19:02

John Coleman