I realy think you are on a completely non-R way of implementing
something that could be done much better. But since I do not know what
you are going to do, here is the answer for your question:
Just add
names(end) <- end
one line before your return() statement.
Uwe Ligges
On 03.02.2011 01:55, Sébastien Bihorel wrote:
Dear R-users,
I would like to have some advises about a problem illustrated by the
following snippet. Within myf, I need to evaluate a piece of R code that is
passed as a character argument and then return the objects that are created
by this code. The difficulty comes from the fact that the content of the
code is variable and unknown to me (obviously not in this illustration!).
With the following script, myf returns the content of the objects created by
the code, but I could not find a way to get the names of the objects in
addition to their contents.
Any suggestion will be welcome
Sebastien
rm(list=ls(all=T))
code1<- paste('l<- list(a=1,b=2,c=3)',
'm<- matrix(1:15,nrow=3)', sep='\n')
code2<- 'g<- rnorm(100,10,2)'
myf<- function(code=NULL){
start<- ls(all=T)
eval(parse(text=code))
end<- ls(all=T)
end<- end[-which(end=='start')]
return(lapply(end[!end%in%
start],function(x) eval(parse(text=x))))
}
myf(code1)
myf(code2)
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
______________________________________________
R-help@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.