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]]

