[R] how can I attach a variable stored in

john seers (IFR) john.seers at bbsrc.ac.uk
Fri Sep 21 15:55:06 CEST 2007



Hi Peter

Perhaps "get" is what you need?

    foo.bar <- list( "a"= "a", "b"=1 )
    save( file="foo.bar.RData", foo.bar )
    rm( foo.bar )

    my.fn <- function( fname ) {
       load( fname )
        attach( get(ls( pat="foo" )) ) #  works
         #attach( foo.bar )  # works
    } 

Regards

JS


 -----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Peter Waltman
Sent: 20 September 2007 20:24
To: Leeds, Mark (IED)
Cc: r-help at r-project.org
Subject: Re: [R] how can I attach a variable stored in

Hi Mark -

Thanks for the reply.  Sorry I didn't really clarify too well what I'm
trying to do.  The issue is not that I can't see the variable that gets
loaded. 

The issue is that the variable is a list variable, and I'd like to write
a function that will take the .RData filename and attach the variable it
contains so that I can more easily access its contents, i.e.

    foo.bar <- list( "a"= "a", "b"=1 )
    save( file="foo.bar.RData", foo.bar )
    rm( foo.bar )

    my.fn <- function( fname ) {
       load( fname )
       attach( ls( pat="foo" ) ) #  I want to attach( foo.bar ), but
    this doesn't work
    }

    ls()  # prints out "foo.bar"

    attach( ls( ) )   # still doesn't work
    attach( foo.bar )  # works

So, basically, the question is how can I attach the variable that's
stored in a file if I don't already know it's name?

Thanks again!

Peter



More information about the R-help mailing list