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?
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...
whenvalueslist=
is defined you could do the following, noting that thevalueslist=NULL
appears after the...
otherwise you end up with the situation where any unnamed argument appearing 5th will be assigned tovalueslist
: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
...
:For example the last yields the following:
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.