How and Where R Stores Your Data


R stores all the objects from a particular database in a single file called .RData. (Notice the leading dot in that name.) That can be handy, since everything is in one place, but it also means that if that file gets lost or corrupted you can be in trouble. Also the .RData file can get awfully big, awfully fast.

The Search Path

Whenever you look for any S-Plus object -- anything you've created or anything built into S-Plus, even the "+" command -- the system goes looking for it in a series of directories. Some of these directories are called "chapters," others "libraries," and other directories are neither of these.

You can see the set of directories S-Plus looks for with the search() command. It looks in each directory, in order, so that if you created your own command named "+" -- and let me say I don't recommend it -- it would "mask" the existing "+" command and yours would be used. The masked() command reveals the names of items that appear in more than one directory. (If you're inside a function, there are other places to look. See memory frames.)

The directory at the top of the list is called the working directory. This is where new objects will be created. (Therefore it needs to be writeable.)

You can manipulate the search path with the attach() and detach() commands. Of course you don't want to detach any of the built-in directories, but you might imagine attaching a directory for a specific project, doing a few things, then detaching it when you're done. Here's an example:

> attach ("d:/marines", pos=1)        # Marine data now accessible
> attach ("d:/marinebkup", pos=2)     # This one is in position 2
> newdata <- newdata                  # Copies an object from position 2 to position 1
> ls(,2)                              # Show objects in directory 2
> detach (2)                          # Detach backup directory

Notice that in S-Plus 6 you attach a directory that contains a .data directory. In earlier versions you attach the _data directory itself.

If you want to create your own directory to store stuff, you need to create it with S-Plus, not just under Windows. (This is new in S-Plus 6.0). Call createChapter() to do this: you only have to do it once. (In addition to setting the directory up for S-Plus, createChapter() will compile any C, C++ or Fortran code it finds lying around in that directory.)

A library is a chapter created by someone else. The files in there look a little different from the ones we make, but everything else acts the same. S-Plus ships with a number of libraries (Mass, class, nnet, and so on). You attach these with the library() command, which has essentially the same effect as the as the attach() command. By default attach() and library() put new directories into position 2, right underneath your working directory,

Using Chapters and Libraries (GUI)

Once you know the command-line story, attaching chapters and libraries from the GUI is simple. In the Object Explorer, find the "SearchPath" icon in the left pane. Right-click on it and you can create a new chapter or attach an existing one. I don't know how to detach a chapter from the GUI, though: I believe you need the command-line detach() function for that.

Libraries are attached with File | Load Library.

Keeping your S-Plus Projects Separate

Each of your S-Plus projects can build up over time, and it's a good idea to keep them separate so you don't accidentally overwrite data from one with data from another. There are at least three ways to do this.
  1. In your home directory create little functions that call attach(). For example, one such function might look like this:
    > manpower
    function(i = 1) {
        attach ("d:/data/manpower/", pos=i)
        invisible() # don't return anything
    When I'm ready to start working on manpower, I type manpower() and that directory is attached.

  2. Create a separate icon on your desktop for each project. Edit the shortcut so that the target line includes a line like S_PROJ="d:\data\manpower" (with a single Windows-style backslash). Double-clicking on the icon will start in the proper project directory.

  3. Under Options | General Settings, Startup tab, click Prompt for Project Folder. Then every time S-Plus starts up, you will be prompted for the folder containing the .data directory. If there isn't one S-Plus will create it.

Return to S-Plus docs