Try this:

Using 'envir' argument, in ls and get function:

get.vars.name.prefix<-function(prefix, envir = parent.frame()){
   result<-list()
   len<-nchar(prefix)
   var.names<-ls(envir = envir,pattern=prefix)
   print(var.names)
   for(i in 1:length(var.names)){
      name<-var.names[i]
      field<-substr(name,len+1,nchar(name))
      result[[field]]<-get(name, envir = envir)
  }
 result
}

On Mon, Oct 20, 2008 at 4:06 AM, erwann rogard <[EMAIL PROTECTED]>wrote:

> hi,
>
> get.vars.name.prefix<-function(prefix){
>  result<-list()
>  len<-nchar(prefix)
>  var.names<-ls(name=1,pattern=prefix) #name=1 probably wrong option
>  print(var.names)
>  for(i in 1:length(var.names)){
>  name<-var.names[i]
>  field<-substr(name,len+1,nchar(name))
>  result[[field]]<-get(name)
>  }
>  result
> }
>
> for example
> x.1<-1
> x.2<-2
> get.vars.name.prefix("x.") should return 1,2
>
> this does not work if i call if from within another function:
>
> g<-function(){
>  x.1<-1
>  x.2<-2
>  get.vars.name.prefix("x.") # should return 1,2, nothing else
> }
>
> i would like the environment specified within ls(...) to be that of the
> body
> of the function that calls get.vars.name.prefix. apparently name=1 is
> probably not the right option.
>
> thanks.
>
>        [[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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[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.

Reply via email to