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

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Thu Nov 23 17:08:35 CET 2006


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



More information about the R-help mailing list