[R] Deparsing part of a list argument
Steven McKinney
smckinney at bccrc.ca
Sat Oct 27 04:25:43 CEST 2007
This is one way to implement the idea
the other responses point at:
> myfun <- function(funlist, vec){
+ tmp <- lapply(funlist, function(x)do.call(x, args = list(vec)))
+ names(tmp)[names(tmp) == ""] <-
+ sapply(match.call()[[2]], deparse)[-1][names(tmp) == ""]
+ tmp
+ }
>
>
> myfun(list("Summation" = sum, prod, "Absolute value" = abs), c(1, 4, 6,
+ 7))
$Summation
[1] 18
$prod
[1] 168
$`Absolute value`
[1] 1 4 6 7
Steven McKinney
Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre
email: smckinney +at+ bccrc +dot+ ca
tel: 604-675-8000 x7561
BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada
-----Original Message-----
From: r-help-bounces at r-project.org on behalf of Andrew Smith
Sent: Thu 10/25/2007 2:38 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Deparsing part of a list argument
Here's a simple example of the type of function I'm trying to write,
where the first argument is a list of functions:
myfun <- function(funlist, vec){
tmp <- lapply(funlist, function(x)do.call(x, args = list(vec)))
names(tmp) <- names(funlist)
tmp
}
> myfun(list("Summation" = sum, prod, "Absolute value" = abs), c(1, 4, 6, 7))
$Summation
[1] 18
[[2]]
[1] 168
$`Absolute value`
[1] 1 4 6 7
What I would like is for the names of the result to be the same as the
names of funlist (as is the case in components 1 and 3 above), but if
some components of funlist are unnamed, I would like the default name
in the result to be the name of the actual function used. So in the
above example, I want the name of the second component to default to
'prod', since funlist[[2]] has no name.
But the problem is that I can't get the function names corresponding
to the unnamed elements of funlist by using the usual
deparse(substitute)) trick, since they're part of a list which is
treated as one big argument. So deparse(substitute(funlist)) just
gives me one big character string of all the functions, and I'm not
sure how I can use that to get just the functions that aren't named.
Any ideas?
Thanks,
Andrew
______________________________________________
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