1
functionabc <- function(api_key, URL, columnNames, globalParam, ...) {
  # code
}

My function currently takes in certain values through the ... (there can be 1 or more). It then stores all of them in a list. I now want to make globalParam optional with a default value of "" and I also want the option of having a parameter called valueslist which would be a list of lists. If the user specifies the value of the parameter valueslist, the user would not enter multiple values through the ...

This is how I want my function to look...

functionabc <- function(api_key, URL, columnNames, globalParam = "", valueslist = NULL, ...) {
  # code
}

How is this function supposed to be called and how do I deal with it? Also, do let me know if there's a better way to do what I am trying to do.

How do you not specify globalParam and valueslist, but pass in the ... set of arguments?

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Ritika R
  • 77
  • 1
  • 1
  • 9
  • What's wrong with good old `if(is.null(valueslist))`? – shadowtalker Jun 25 '15 at 18:37
  • @ssdecontrol nothing! My question then is how do you not specify globalParam and valueslist, but pass in the ... set of arguments? – Ritika R Jun 25 '15 at 18:53
  • Just... don't specify them? Or are you looking for the `missing` function? Your code in "this is how I want my function to look" is perfectly valid, so it isn't clear what more you need. – shadowtalker Jun 25 '15 at 19:17

1 Answers1

2

I think the "optional" parameters, the ones with global defaults, should not impact how you deal with what is passed in the ... set of arguments. That said, you have to be very careful of R's lazy evaluation and partial argument matching. If you need to combine these and/or ignore the ... when valueslist= is defined you could do the following, noting that the valueslist=NULL appears after the ... otherwise you end up with the situation where any unnamed argument appearing 5th will be assigned to valueslist:

functionabc <- function(api_key, URL, columnNames, globalParam="", ..., valueslist=NULL) {
  namedlist <- list(
    "api_key"=api_key, 
    "URL"=URL, 
    "columnNames"=columnNames, 
    "globalParam"=globalParam
  )

  if (is.null(valueslist)) {
    valueslist <- list(...)
  }

  print(ls())
  flush.console()
  list("namedlist"=namedlist, "valueslist"=valueslist)

  ### code
}

Calling the function like this should all yield logical results, but again, be careful of partial matches in optional, named arguments that you expect to end up in ...:

functionabc(1,2,3,4,x=5,y=6,z=7)
functionabc(1,2,3,4,5,6,7,8,9)
functionabc(1,2,3,4,5,6,7, valueslist=list(99,100,101))

For example the last yields the following:

> functionabc(1,2,3,4,5,6,7, valueslist=list(99,100,101))
[1] "api_key"     "columnNames" "globalParam" "namedlist"  
[5] "URL"         "valueslist" 
$namedlist
$namedlist$api_key
[1] 1

$namedlist$URL
[1] 2

$namedlist$columnNames
[1] 3

$namedlist$globalParam
[1] 4


$valueslist
$valueslist[[1]]
[1] 99

$valueslist[[2]]
[1] 100

$valueslist[[3]]
[1] 101

You might also check to make sure that what's being passed in valueslist is actually a list object at that stage as well, depending on your needs.

Community
  • 1
  • 1
Forrest R. Stevens
  • 3,435
  • 13
  • 21
  • Hmm how would you call the function then? `code` functionabc <- function(api_key, URL, columnNames, globalParam="", valueslist=NULL, ...) { if (is.null(valueslist)) { valueslist <- list(...) } print(api_key) print(URL) print(columnNames) print(globalParam) print(valueslist) } `code` I called functionabc(1,2,3,4,5,6) and valueslist turned out to be 5. How do you not specify globalParam and valueslist, but pass in the ... set of arguments? – Ritika R Jun 25 '15 at 18:44
  • To be the most careful they would need to be named arguments. What you might be running into is R's "lazy" evaluation and partial argument matches. This can be extremely confusing if you're coming to R from another language. Beyond the potential partial match issue it is important that you know how the arguments are being converted to variables in the functions. I've expanded my code and corrected a typo above, hopefully it's more clear! – Forrest R. Stevens Jun 25 '15 at 19:39