pass named list to ellipses in R function

30 Views Asked by At

I have written a function funA, which takes an unknown number of inputs and performs some calculations. The inputs are generated by another function funB, which returns a named list. Ideally, I would like to pass this named list straight to funA. However, the way I have written funA this causes an error... Here is an example function which illustrates the problem:

sameLength <- function(...) {
  vecs <- list(...)
  numVecs <- length(vecs)
  if ( numVecs <= 1 ) {
    return(cat("Need two or more vectors."))
  }
  allSame <- TRUE
  len <- length(vecs[[1]])
  for ( i in 2:numVecs ) {
    if ( length(vecs[[i]]) != len ) {
      allSame <- FALSE
      break
    }
  }
  allSame 
}

output_funB <- list("a"=1:3,"b"=1:3, "c"=1:4)

sameLength(a = output_funB$a, b=output_funB$b, c=output_funB$c) # this works as intended, but is very cumbersome

sameLength(output_funB) # Ideally this is how the function should work

I am sure it is very easy to accomplish what I am trying to do, but I have not found a solution yet... simply changing vecs <- list(...) to vecs <- ... did not work unfortunately.

Any help is much appreciated!

0

There are 0 best solutions below