Here's a function that does what I think you want to do: > attach.firstvar <- function(file) { + tmpenv <- new.env() + vars <- load(file, envir=tmpenv) + x <- get(vars[1], envir=tmpenv, inherits=FALSE) + if (is.list(x)) + attach(x, name=vars[1]) + return(vars) + } > x <- list(xa=1, xb=2, xc=3) > save(list="x", file="tmp1.rda") > remove(list="x") > attach.firstvar("tmp1.rda") [1] "x" > ls(pos=2) [1] "xa" "xb" "xc" > find("xa") [1] "x" > search() [1] ".GlobalEnv" "x" "package:stats" [4] "package:graphics" "package:grDevices" "package:utils" [7] "package:datasets" "package:methods" "Autoloads" [10] "package:base" > xa [1] 1 > xb [1] 2 >
Peter Waltman wrote: > 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 > > Leeds, Mark (IED) wrote: >> I don't think I understand your question but John Fox has written a very >> nice documentat about scoping and environments on his website. >> It's probably easy to find the site by googling "John Fox" but, if you >> can't find it, let me know. >> >> As I said, I don't think that I understand your question but, if you >> loaded a list variable using load("whatever.Rdata"), the variable will >> just be suitting in your workspace. You don't need to attach anything >> because load just loads the data right into the workspace. >> So typing the variable name should show the data. >> >> >> >> >> -----Original Message----- >> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] >> On Behalf Of Peter Waltman >> Sent: Thursday, September 20, 2007 1:32 PM >> To: r-help@r-project.org >> Subject: [R] how can I attach a variable stored in >> >> Hi - >> >> Any help would be greatly appreciated. >> >> I'm loading a list variable that's stored in an .RData file and would >> like attach it. >> >> I've used attach( <file_name> ), but that only lets me see the variable >> that's stored in the file. >> >> As the variable name is of the form "comp.x.x", I've tried using attach( >> ls( pat="comp" ) ), but get an error as ls() just gives back a string. >> >> I've also played around with eval(), but don't really quite get what >> that function does since it seems to get into the R internals which I >> don't entirely understand and I haven't found any great unified >> documentation on R's handling environment and scoping. >> >> Thanks, >> >> Peter Waltman >> >> ______________________________________________ >> 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. >> -------------------------------------------------------- >> >> This is not an offer (or solicitation of an offer) to buy/sell the >> securities/instruments mentioned or an official confirmation. Morgan >> Stanley may deal as principal in or own or act as market maker for >> securities/instruments mentioned or may advise the issuers. This is not >> research and is not from MS Research but it may refer to a research >> analyst/research report. Unless indicated, these views are the author's and >> may differ from those of Morgan Stanley research or others in the Firm. We >> do not represent this is accurate or complete and we may not update this. >> Past performance is not indicative of future returns. For additional >> information, research reports and important disclosures, contact me or see >> https://secure.ms.com/servlet/cls. You should not use e-mail to request, >> authorize or effect the purchase or sale of any security or instrument, to >> send transfer instructions, or to effect any other transactions. We cannot >> guarantee that any such requests received v i! > a e-mail will be processed in a timely manner. This communication is solely > for the addressee(s) and may contain confidential information. We do not > waive confidentiality by mistransmission. Contact me if you do not wish to > receive these communications. In the UK, this communication is directed in > the UK to those persons who are market counterparties or intermediate > customers (as defined in the UK Financial Services Authority's rules). >> > > ______________________________________________ > 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.