Why is (...) useful in R? [duplicate] - r

This question already has answers here:
How to use R's ellipsis feature when writing your own function?
(5 answers)
Closed 8 years ago.
I'm trying to understand ... and/or (...) in R. I understand that somehow this is used for entering unknown or multiple parameters to a function, but when is this necessary and/or useful? I've searched rdocumentation for it, but found nothing. In the R Language Definition it is defined but in very abstract terms.
Hence I ask: why is ... useful? Is it just not sloppy coding? Wouldn't it be better to pass arguments explicitly?

It's called varargs (short for variadic arguments).
when is this necessary?
It's not strictly necessary, and it's not inherently sloppy. The "non-sloppy" alternative to varargs in any language is to pass an array or list as a single variable. So varargs is just syntactic sugar on top of a collection of things.
[when is it] useful?
Any time you want to save a few keystrokes and implicitly construct a list when calling a function.
Wouldn't it be better to pass arguments explicitly?
Depends. What are your criteria?

The ellipsis gives you the possibility to define functions with an unknown number of arguments/parameters.
It necessary for functions like c or list where the number of arguments given by the user is unknown.
If you type c or list in the R console you'll see that both of these functions use ... as arguments.
In these two cases it would be hard to pass arguments explicitly since the user can pass as many arguments as needed.
You can look at this post for more examples

Related

How can I see all of the valid options for an argument in an R function?

When I'm using a new function, the documentation/vignettes give an overview of how the function works, but often do not list all of the possible options for every argument.
There must (?) be a command which lists all of the valid options for an argument? After lots of Googling there are lots of answers to the question of how to list all the possible arguments of a function (args, formals etc), but nothing I can find which lists all the possible options of an argument?
For example if I type formals(lmer) or args(lmer), it just list the functions and the defaults, not all the possible options.
How can I display all the possible options that an argument can take?

Suppressing parentheses in a function that uses the pipe operator

Can somebody explain why I have to suppress the parentheses for the function is.factor in the command shown below? Student-data was read from a .csv file. I can see the structure of Student-data and I want to select only the factor variables. The command works fine but I cannot see why I cannot write the parentheses. I saw an example in the forum. Sorry if the question is silly or has been asked before. I could not find any similar question.
studentData%>%select_if(is.factor)
It's not the pipe, %>%, that requires you to "drop the brackets, it's select_if. From the documentation:
.predicate "A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names."
You're not evaluating the function here. You're passing an R object. (Functions are objects, just as data.frames or scalars are). The evaluation happens later, in the guts of select_if. Including the brackets would tell R to evaluate the function at the time the select_if call was executed. That's not correct. It needs to be evaluated later.

Is attributes() a function in R?

Help files call attributes() a function. Its syntax looks like a function call. Even class(attributes) calls it a function.
But I see I can assign something to attributes(myobject), which seems unusual. For example, I cannot assign anything to log(myobject).
So what is the proper name for "functions" like attributes()? Are there any other examples of it? How do you tell them apart from regular functions? (Other than trying supposedfunction(x)<-0, that is.)
Finally, I guess attributes() implementation overrides the assignment operator, in order to become a destination for assignments. Am I right? Is there any usable guide on how to do it?
Very good observation Indeed. It's an example of replacement function, if you see closely and type apropos('attributes') in your R console, It will return
"attributes"
"attributes<-"
along with other outputs.
So, basically the place where you are able to assign on the left sign of assignment operator, you are not calling attributes, you are actually calling attributes<- , There are many functions in R like that for example: names(), colnames(), length() etc. In your example log doesn't have any replacement counterpart hence it doesn't work the way you anticipated.
Definiton(from advanced R book link given below):
Replacement functions act like they modify their arguments in place,
and have the special name xxx<-. They typically have two arguments (x
and value), although they can have more, and they must return the
modified object
If you want to see the list of these functions you can do :
apropos('<-$') and you can check out similar functions, which has similar kind of properties.
You can read about it here and here
I am hopeful that this solves your problem.

R: What does `rm(list=ls())` do? [duplicate]

This question already has answers here:
What is the difference between rm() and rm(list=ls())?
(2 answers)
Closed 4 years ago.
While looking at some R code written by someone else, I find near the top of the document rm(list=ls()). I tried looking up what the rm function does and as far as I can tell it deletes specified variables from the workspace, so that if you had x=3 somewhere, then after running rm(x) the variable x would behave as if it had never been assigned (so for instance would throw an error if you tried to print it).
But then the rest of the code is strange if I have this right, because it seems to be removing the empty list. So it's not doing anything at all, right? Is there a reason to have this code here?
I think the parameter list for the rm function is confusing, at least it was for me when I started using R. The list parameter is not actually supposed to be a list but rather a character vector. The ls-function does return a character vector of all the named objects visible in the environment where it is called and it has a default value that gets used if no other value is offered for its envir argument. If you do this at the console, then the default environment is the global environment. So this will clear all the "visible" objects (but not the ones defined in other namespaces or environments like the ones that exist in the base, graphics, stats or other loaded package-namespaces.
So now go look at ?ls and ?rm to better understand their capabilities. In particular new useRs should get their heads clear on the differences between R names, i.e. symbols, versus the character representations of them. The ls function is really reaching into the language level of R's implementation and returning a non-language-level value, whereas rm typically takes a language level input ... unless (as in this case) it is offered a value to its "list"-argument, ... which is not an R list. Clear? Maybe, hopefully, a bit more so.
It clears all objects from the workspace.

In help files, is there any universal meaning or logic behind the "..." arguments?

I notice in different packages it sometimes refers to a variable, a column to sort by, an object, etc. Like in dplyr it usually refers to a variable, but in ggplot, it's not even used.
Is there any logic behind this? Is there any universality? Or can it just be anything.
It can accept an arbitrary number of formal parameters. In base R these parameters are often arguments to other functions. The Arguments description in the function's help page should indicate which subsequent functions will be getting these arguments. It can be the only argument, the first, the last, or it can be intercalated in the parameter list. The items in the list(...) generally should be named. You can access the items in the "dots" list in a couple of different ways: ...() and list(...) are two that I've seen. Generally, it is the R-cognoscenti that will be designing functions with these forms. When it is intercalated (or the first), the named parameters that follow it in the formals must be named when the function is called and cannot be assigned positionally. If you type ?'...' you will be shown the Reserved page, which in turn has a link to dotsMethods {methods}.
I found it difficult to search for [r] with "..." using a SO search panel but searching on "[r] dotsMethods" brought up 10 hits.

Resources