[R] Workspaces & Memory Requirements

Thomas Lumley thomas at biostat.washington.edu
Sun Dec 10 23:00:59 CET 2000

On Sun, 10 Dec 2000, Dilip N. Thadani wrote:

> Can someone explain memory and workspaces in R.  For example, I read in the
> FAQ (or somewhere) that in R, the entire workspace is loaded into memory.

> So if I use and save many data frames or matrices in my workspace, are all
> of these objects getting loaded into memory each time?


>  If so this is very
> inconvenient if one wants to store data in matrices on an ongoing basis but
> not use them everytime one uses R.

The objects will get paged out to virtual memory (with the new garbage
collector and a good OS) if they aren't ever used, but there is still some
> So how should one manage objects in R.  Should one be using multiple
> workspaces or should one write a couple of functions that dump objects out
> into text files and then source them back before use and so on.

Data can be stored with save()/load(), which is faster than dump()ing into
text files. 

> Is the new dynamic memory model upcoming in the next release of R going to
> also read all objects in a workspace into RAM?

Yes. As mentioned above it is done a bit more efficiently now.

> Also, how do I take all of my user written R functions and make them into a
> library so that they can be accessible from any workspace?  And can these
> functions be edited and saved on the fly into the library?

You can make a package as described in the "Writing R Extensions" manual.
Basically you need a directory with subdirectories R/ for R code and man/
for documentation and a DESCRIPTION file. After being installed with R
INSTALLL the package can be loaded with library() and unloaded with

You can also save() functions and/or data to a file and attach() the file,
and detach() it when you're done. This is similar to attach()ing a
directory in S-PLUS.


Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list