R programming

ondansetron

TS Contributor
#1
This is a module in the Swirl package that I've had some trouble understanding their explanation, although I'm able to successfully complete the assigned task. I was hoping someone would provide a better explanation of what's going on in the following:

# Let's explore how to "unpack" arguments from an ellipses when you use the
# ellipses as an argument in a function. Below I have an example function that
# is supposed to add two explicitly named arguments called alpha and beta.
#
# add_alpha_and_beta <- function(...){
# # First we must capture the ellipsis inside of a list
# # and then assign the list to a variable. Let's name this
# # variable `args`.
#
# args <- list(...)
#
# # We're now going to assume that there are two named arguments within args
# # with the names `alpha` and `beta.` We can extract named arguments from
# # the args list by using the name of the argument and double brackets. The
# # `args` variable is just a regular list after all!
#
# alpha <- args[["alpha"]]
# beta <- args[["beta"]]
#
# # Then we return the sum of alpha and beta.
#
# alpha + beta
# }
 
#2
Lists allow you to store multiple types of objects in the same place. They can hold character vectors, data frames, booleans etc. In this case, alpha and beta are numeric values. The module is showing you how to create a function that extracts these values (using double bracket syntax) then add them. Lists are helpful for storing large amounts of information. I recently found use for the rbind_list() which creates a data frame out of the list objects in a given row of a dataset.
 
Last edited:

ondansetron

TS Contributor
#3
Lists allow you to store multiple types of objects in the same place. They can hold character vectors, data frames, booleans etc. In this case, alpha and beta are numeric values. The module is showing you how to create a function that extracts these values (using double bracket syntax) then add them. Lists are helpful for storing large amounts of information. I recently found use for the rbind_list() which creates a data frame out of the list objects in a given row of a dataset.
Thanks for the background @Buckeye and I hope the MStat is going well!

I was also wondering what's happening behind the scenes now.

I get confused because I specify the function(...) which I take the ellipsis to mean "fill in with something" and then I store the list(...) in the object "args", but I still haven't filled in the ellipsis. To me, it then appears that I subsequently store (in alpha and beta objects) the subset item(s) from args (which are called alpha and beta) before adding them. Is the second ellipsis related to the first or is it an independent place holder?
I might be having trouble knowing what order to read the code in with the ellipsis, but I feel as if I haven't defined the ellipsis but then try to pull out "alpha" for example without ever putting "alpha" into args. Also, is "alpha" enclosed in quotation marks because it's an element of args? I initially thought it was weird since it's not a character element?

Does this basically boil down to the arguments of my function coming from a long "possible" list of arguments and this is only a short list for the example? Otherwise, I'm wondering why the ellipsis is needed if this isn't for the purpose of longer possible arguments to specify.

Sorry if I'm not clear. I don't have any programming background prior to R.
 

Dason

Ambassador to the humans
#4
Honestly I don't think that is a good example for learning/understanding what ... is used for. In this specific case I can't see any reason why one wouldn't just include alpha and beta as named parameters.
 

ondansetron

TS Contributor
#5
Honestly I don't think that is a good example for learning/understanding what ... is used for. In this specific case I can't see any reason why one wouldn't just include alpha and beta as named parameters.
Would you have an example on hand, or a better explanation than they provided, to help me get a better understanding of what's going on in this setup?

I completed the module but don't believe I know anything about this setup. The module is a general intro to functions.
 

Dason

Ambassador to the humans
#6
I honestly don't quite 'get' what they were trying to illustrate with their example so I'm not sure it makes sense to analyze that particular example any further. The dots parameter is useful for a few things but what they used it for is... pointless. The two main uses for in my opinion are when you want to allow a user to pass additional parameters/input into functions that you're going to use in your function and don't want to provide a named parameter for every possible thing they could pass in. For example if you're making something that calls `plot` you might want to allow the user to change the options specified within plot. So in that case maybe you would do something like

Code:
my_log_plotting_function <- function(x, y, ...){
  # Do some stuff before passing to plot
  y <- log(y+1) # just cause

  plot(x, y, main = "This is my special title", ...)
}
and now to run it

Code:
x <- 1:10
y <- rexp(10)-1
# just the normal call
my_log_plotting_function(x, y) 
# What's going on? I didn't define a 'xlab' parameter in my function
my_log_plotting_function(x, y, xlab = "My x label")
Another nice use is when you don't know exactly how many items are going to be passed in so you can't necessarily create a parameter for each. In those cases it might make more sense to have a single input and force that input to be a list instead of relying on the dots but you can use the dots in this way. It's not something you will probably need to worry about any time soon. Just know that it exists. For example the paste function uses that functionality.

Code:
> # ... is the first parameter to paste
> args(paste)
function (..., sep = " ", collapse = NULL) 
NULL
> paste("trinker")
[1] "trinker"
> paste("trinker", "smells")
[1] "trinker smells"
> paste("trinker", "smells", "like butts")
[1] "trinker smells like butts"
> paste("trinker", "smells", "like butts", sep = "! ")
[1] "trinker! smells! like butts"