[R-SIG-Mac] Help request w/ function argument names

Steven McKinney smckinney at bccrc.ca
Thu May 15 05:40:46 CEST 2008


Hi Carl,

> Hi,
> I've been trying to figure out if there's a way to return the actual
> names of the arguments passed to a function which uses ellipsis.
> 
> There's an example in Vincent Zoonekynd's <zoonek at math.jussieu.fr>
> manual that looks like this:
> 
> f <- function (...)
> {
> l <- list(...) # Put the arguments in a (named) list
> for (i in seq(along=l))
> {
> cat("Argument name:", names(l)[i], "Value:", l[[i]], "
> \n")
> }
> }
> 
> 
> I don't know exactly what he thought would happen (or what happens from
> the command line?) but from the Mac Aqua GUI, names(l) evaluates to
> NULL, or possibly the 'names' attribute of the first element of the
> first argument passed to the function, if such exists.   And in fact 'l'
>   contains the values of all elements of the arguments to f.


This function only works for arguments which are assigned a value that
exists in scope.


> exists("foo")
[1] FALSE
> exists("bar")
[1] FALSE
> 
> f <- function (...)
+ {
+   l <- list(...) ## Put the arguments in a (named) list
+   for (i in seq(along=l))
+     {
+       cat("Argument name:", names(l)[i], "Value:", l[[i]], "\n")
+     }
+ }
> f(foo = 1, bar = 2)
Argument name: foo Value: 1 
Argument name: bar Value: 2 
> f(foo = bar, bar = 2)
Error in f(foo = bar, bar = 2) : object "bar" not found
> f(foo = 2, bar = foo)
Error in f(foo = 2, bar = foo) : object "foo" not found
> f(foo = baz, bar = 2)
Error in f(foo = baz, bar = 2) : object "baz" not found
> f(foo, bar = 2)
Error in f(foo, bar = 2) : object "foo" not found
> f(foo, bar)
Error in f(foo, bar) : object "foo" not found
> 

The first call to the function worked as the arguments were assigned
known values.  The others did not as an unknown object was encountered.


> 
> So, is there any way to have the function 'f'  "know" what the names are
> of the arguments passed to it?  That is, if I type
> 
> 
>  > f(foo,bar)
> 
> I would like to be able to have the strings 'foo' and 'bar' available
> somehow.

The way to do this is with the match.call() function, and the
deparse() function.  (Put a browser() command inside this function and
you can poke around the various pieces.)

The call object returned by match.call() will have no names if the
arguments comprising ... have no assignments.  The call object
components can be extracted using the list element extractor [[ ]] and
the deparse() wrapper ensures the parser does not try to evaluate the
possibly non-existent argument objects.  (I skipped the first element
of the match.call() return object, which is the name of the function.)



> f <- function (...) {
+   mc <- match.call()
+   mct <- deparse(substitute(mc))
+   ## browser() ## uncomment this browser command to start your evening of fun.
+   cat("\n")
+   cat("deparsed match call:", mct)
+   cat("\n")
+ 
+   if ( length(names(mc)) ) {
+     for ( i in seq(along = names(mc)[-1]) ) {
+       cat("Argument ", i, " name:", names(mc)[i+1], "Value:", deparse(mc[[i+1]]), "\n")
+     }
+   } else {
+     for ( i in seq(length(mc) - 1) ) {
+       cat("Argument ", i, " name:", "", "Value:", deparse(mc[[i+1]]), "\n")
+     }
+   }
+ }
> 
> f(foo, bar)

deparsed match call: f(foo, bar)
Argument  1  name:  Value: foo 
Argument  2  name:  Value: bar 
> f(foo, bar, baz = 1)

deparsed match call: f(foo, bar, baz = 1)
Argument  1  name:  Value: foo 
Argument  2  name:  Value: bar 
Argument  3  name: baz Value: 1 
> f(foo = 1, bar = 2)

deparsed match call: f(foo = 1, bar = 2)
Argument  1  name: foo Value: 1 
Argument  2  name: bar Value: 2 
> f(foo = bar, bar = 2)

deparsed match call: f(foo = bar, bar = 2)
Argument  1  name: foo Value: bar 
Argument  2  name: bar Value: 2 
> f(foo = 2, bar = foo)

deparsed match call: f(foo = 2, bar = foo)
Argument  1  name: foo Value: 2 
Argument  2  name: bar Value: foo 
> f(foo = baz, bar = 2)

deparsed match call: f(foo = baz, bar = 2)
Argument  1  name: foo Value: baz 
Argument  2  name: bar Value: 2 
> 

My session info:

> sessionInfo()
R version 2.7.0 (2008-04-22) 
powerpc-apple-darwin8.10.1 

locale:
C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
> 



Hope this helps.

Steve McKinney


> 
> Any advice or comments greatly appreciated.
> 
> Carl
> 



More information about the R-SIG-Mac mailing list