How to get list value (...) in functions R and S

I am confused about how list(...) works in R and S-plus. For the following code

 pp <- function(x, ...) { print( length( list(...)))} pp(1,,,1) 

It works in S-Plus, but in R it gives " Error in print(length(list(...))) : argument is missing, with no default "

I'm more interested in how this works in R and how to get the value of list(...) in R functions.

+4
source share
2 answers

I'm not sure why this syntax is allowed in S-plus, but not in R.

Here, however, there is some R code that will have essentially the same effect:

 pp <- function(x, ...) { print(length(as.list(match.call(expand.dots=FALSE))[["..."]])) } pp(1,,,1) # [1] 3 

Alternatively, using the trick from here :

 ppp <- function(x, ...) { print(length(substitute(...()))) } ppp(1,,,1) # [1] 3 
+4
source

You cannot use arguments without a name (edit: ... and missing) in dots and any arguments after dots when these arguments are intended to be matched in an arg list. Position matching, which automatically occurs for unnamed arguments, only "works" in the typical argument processing for named arguments (in the argument list) before points.

 > pp1 <- function(x, ...) { length( list(...))} > pp1(1,z=NULL,xx=NULL,1) [1] 3 > pp2 <- function(x, z, ...) { length( list(...))} > pp2(1,z=NULL,xx=NULL,1) [1] 2 > pp3 <- function(x, z, ...) { length( list(...))} > pp3(1, ,xx=NULL,1) [1] 2 > pp <- function(x, ...) { length( list(...))} > pp(1, , xx=NULL, 1) Error in pp(1, , xx = NULL, 1) : argument is missing, with no default 

When reading the help page for match.call, the second “commonly used circumstance” is described as:

To pass most of the call to another function, often model.frame. Here the common idiom is that expand.dots = FALSE is used and the element ... of the matching call is removed.

The sequence of argument matching (if not bypassed) is described in Section 4.3.2, “Argument Matching”:

  • Coordination of positions. Any unsurpassed formal arguments are tied to the unnamed provided arguments in order. If there is an argument "...", it will accept the remaining arguments, tagged or not.

If any arguments remain inconsistent, an error is declared.

0
source

Source: https://habr.com/ru/post/1484920/


All Articles