[R] reaccessing array at a later date - trying to write it to file

Henrik Bengtsson hb at stat.berkeley.edu
Fri Nov 24 05:52:49 CET 2006


Here is a simple function that allow you to load the objects stored by
save() into an evironment (to avoid loading them into the global
workspace):

loadToEnv <- function(...) {
  env <- new.env()
  load(..., env=env)
  env
}

x <- 1:10
save(file="foo.xdr", x, letters, R.version)
objects <- loadToEnv("foo.xdr")
> ls(env=objects)
[1] "letters"   "R.version" "x"
> objects$x
 [1]  1  2  3  4  5  6  7  8  9 10

and so on.

/Henrik

On 11/24/06, Jenny Barnes <jmb at mssl.ucl.ac.uk> wrote:
> Thank you Barry for your time in responding!
>
> I think that will really help - the difference between attach and load were not
> clear to me before your reply! Also I did not know about rm() - thank you for
> the detail, I know you took longer than you had planned but I do appreciate it,
>
> For those with a similar problems in the future please see the responses below:
>
> >
> >Jenny Barnes wrote:
> >
> >> Having tried again your suggestion of load() worked (well - it finished,
> which I
> >> assume it meant it worked). However not I am confused as to how I can check
> it
> >> has worked.
> >> I typed
> >>
> >>>data.out$data
> >>
> >> which called up the data from the file - but I'm not sure if this is data
> from
> >> the file I have just restored as in my "previously saved workspace restored"
> >
> >  Remove it from your current workspace:
> >
> >  > rm(data.out)
> >
> >  then do the load('whatever') again:
> >
> >  > load("/some/path/to/data.out.RData")
> >
> >  then see if its magically re-appeared in your workspace:
> >
> >  > data.out$data
> >
> >  But now if you quit and save your workspace it'll be in your workspace
> >again when you start up.
> >
> >  So you could consider 'attach' instead of 'load'...
> >
> >  Remove data.out from your current workspace, save your current
> >workspace (with 'save()' - just like that with nothing in the
> >parentheses), then instead of load('/some/path/to/data.out.RData') use:
> >
> >  > attach('/some/path/to/data.out.RData')
> >
> >  This makes R search for an object called 'data.out' in that file
> >whenever you type 'data.out'. It will find it as long as there's not a
> >thing called 'data.out' in your workspace. So if you do attach(...) and
> >then do:
> >
> >  > str(data.out)
> >
> >  you'll see info about your data.out object, but then do:
> >
> >  > data.out=99
> >  > str(data.out)
> >
> >  you'll see info about '99'. Your data.out is still happily sitting in
> >its .RData file, its just masked by the data.out we created and set to
> >99. Delete that, and your data.out comes back:
> >
> >  > rm(data.out)
> >  > str(data.out) # - your data object again
> >
> >  The advantage of this is that data.out wont be stored in your current
> >workspace again. The disadvantage is that you have to do
> >'attach(...whatever...)' when you start R, and that data.out can be
> >masked if you create something with that name in your workspace. It is a
> >handy thing to do if you create large data objects that aren't going to
> >change much.
> >
> >>  Also, is it normal that if I type
> >>
> >>>data.out.RData
> >>
> >> it says
> >> Error: object "data.out.RData" not found
> >
> >  Yes, because thats the name of the _file_ on your computer and not the
> >R object.
> >
> >  This should be in the R manuals and help files... and I've gone on
> >much longer than I intended to in this email :)
> >
> >Barry
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Jennifer Barnes
> PhD student - long range drought prediction
> Climate Extremes
> Department of Space and Climate Physics
> University College London
> Holmbury St Mary, Dorking
> Surrey
> RH5 6NT
> 01483 204149
> 07916 139187
> Web: http://climate.mssl.ucl.ac.uk
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list