[R] To improve my understanding of workspaces

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Fri Mar 10 16:22:08 CET 2006

A lot of programming style are personal choices and as such varies from
individual to individual. See my comments below.

On Fri, 2006-03-10 at 09:01 -0500, Kevin E. Thorpe wrote:
> Thanks Adai.  A couple questions/comments about this.
> Adaikalavan Ramasamy wrote:
> > I use emacs and ESS to develop the scripts. The new releases of R has
> > the script function already in built.
> I use emacs and ESS too (in Linux).  I do not know about the script
> function you mention.  It's not in my version (2.1.1) and I couldn't
> find it in an RSiteSearch either.

I meant to say in newer releases of R _for Windows only_ has script
function. Look under File->New scripts (untested). But however it does
not appear to have syntax highlighting or auto indenting that emacs has.

> > Typically I keep all the data and scripts related to a project in its
> > own folder, so I have minimal worry about paths.
> I do the same.
> > To save large and associated objects, I use 
> >    save(x, y, z, file="lala.rda", compress=TRUE) 
> > and then to load x, y, z in another session or workspace I use
> >    load("lala.rda") 
> > 
> > To save small dataframes and matrices, I use 
> >    write.table(mat, file="lala.txt", sep="\t") 
> > and to read it back I use
> >    mat <- read.delim(file="lala.txt", row.names=1)
> Am I correct that load() or read.<whatever>() or even data() will
> bring the objects into the current workspace while attach() can
> attach a save() data frame to the search path?  Is one approach
> better than the other in general?

I think you are correct.

The attach function appears to have two functions now :
 a) attach("lala.rda") loads objects from lala.rda into the search path
 b) attach(obj) makes the named columns of a dataframe or list available
in the search path. Therefore you only need to type 'aaa' instead of 
obj$aaa or obj[ , "aaa"]

The second is the more popular form of usage. 

Personally I would rather not use attach() and prefer to type obj$aaa or
use in the context of lm( aaa ~ ., data=obj ).

> > The problem with .RData (via quit or save.image), is that it keeps all
> > intermediate objects which can be unnecessarily bloated and confusing.
> > Further you will have difficulty distinguishing one .RData from the
> > other by looking at the filename alone.
> If you don't save the workspace on q(), do you also lose the history for
> that session (although when working in emacs, this is rarely a problem)?

I would argue that script file is a better way than history files
because I can clean up any test or wrong codes I might have in the
script file.

However if you prefer to save the history, you can use
savehistory(file="history.txt") at any point 

Regards, Adai


More information about the R-help mailing list