[R] getting multiple argument names

Bert Gunter gunter.berton at gene.com
Fri Jun 27 19:50:51 CEST 2008


You evidence a good deal of confusion. V&R's S PROGRAMMING, in particular
Chapter 3 (especially section 3.5 on Computing on the Language) would be
helpful to you. See also their example on p. 46.

However in brief:

foo <- function (...) {
x <-3
list(...)
}
 
returns the evaluated ... arguments as a list. But note that in your call
below, foo(a,b,c), the (actual) arguments tha you give are **unnamed** and
so you would get:

foo(a,b,c)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

If you wanted the list components named, you must name them:

foo(a=a,b=b,c=c)
$a
[1] 1

$b
[1] 2

$c
[1] 3

There are subtleties here that I overlooked(e.g. what happens if the
arguments are expressions with promises, as for lazy evaluation?) As I said,
V&R provide a fairly comprehensive and (to me) accessible overview of these
things. It can be complicated, for sure.


-- Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of whizvast
Sent: Friday, June 27, 2008 8:48 AM
To: r-help at r-project.org
Subject: [R] getting multiple argument names


hi, all-

i wrote a function that accept multiple arguments, but don't know how to
assign names automatically. run the following code:

foo <- function (...) {
  x = list(...)
  names(x) <- deparse(substitute(...))
  x
}
a = 1; b = 2; c = 3
y <- foo( a, b, c)
names(y)

as you can see, only the first items are correctly named. 
how do i correct this problem?



-- 
View this message in context:
http://www.nabble.com/getting-multiple-argument-names-tp18158010p18158010.ht
ml
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list