Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get names of dot-dot-dot arguments in R [duplicate]

Tags:

r

ellipsis

How obtaining a characters vector containing the name of the dot-dot-dot arguments passed to a function e.g.:

test<-function(x,y,...)
{
    varnames=deparseName(substitute(list(...)))
    # deparseName does not exist, this is what I want !
    # so that I could *for example* call:

    for(elt in varnames)
       {print(varnames);}
}

v1=11
v2=10
test(12,12,v1,v2)

## would print 
#v1
#v2
like image 982
user1835313 Avatar asked Jul 10 '18 07:07

user1835313


People also ask

What is the dot dot dot argument in R?

... argument is a special argument useful for passing an unknown number of arguments to another function. This is widely used in R, especially in generic functions such as. plot()

How do I use three dots over it in R?

If you have used R before, then you surely have come across the three dots, e.g. In technical language, this is called an ellipsis. And it means that the function is designed to take any number of named or unnamed arguments. By the way, the ellipsis is not a specialty of R.


2 Answers

Try this:

test<-function(x,y,...)
{
  mc <- match.call(expand.dots = FALSE)
  mc$...
}

v1=11
v2=10
test(12,12,v1,v2)
[[1]]
v1

[[2]]
v2
like image 74
Juan Antonio Roldán Díaz Avatar answered Nov 09 '22 09:11

Juan Antonio Roldán Díaz


You can use deparse and substitute to get what you want (see also this Q&A):

test<-function(x, y, ...)
{
    varnames=lapply(substitute(list(...))[-1], deparse)
    lapply(varnames, print)
    return(invisible())
}

test(12,12,v1,v2)
#[1] "v1"
#[1] "v2"
like image 20
Cath Avatar answered Nov 09 '22 10:11

Cath