![]() SAVING AND LOADING OBJECTS IN R The Workspace and History You've probably noticed already, when you start and quit R, you see
something like this...
Your workspace, remember, consists of all the data objects you've created or loaded during your R session (and perhaps a few other things as well). When R asks if you want to save your workspace image before quitting, and you say "no", all of the NEW stuff you've created goes away--forever. If you say "yes", then a file called ".RData" is written to your working directory. The next time you start R in this directory, that workspace and all its data objects will be restored. This is extremely convenient for people who are running R in a terminal, such as the terminal.app in OS X or a Linux terminal. Those people can devote a working directory to each problem they are working on, and when they start R, they can simply change to that directory FIRST (before starting R), and R will open with that directory as the working directory and restore the workspace from there. It's not so convenient for people who are running R in Windows or in the Mac R GUI console, in which case R will always start in the same default working directory. In Windows XP that will be "My Documents". In Windows Vista and (I suspect) Windows 7, it is the user's home directory. On the Mac and in Linux it will be the user's home directory. (If you don't know what your home directory is, then start R and use getwd( ). Then you'll know!) There is, of course, a work-around. I'll describe how it would work in Windows XP. Mac users, I suspect, will be smart enough to see the immediate generalization. In Windows XP, R opens by default in "My Documents". This default behavior can be changed, but there is little point in doing so. When R opens, it will load the workspace file (".RData") if there is one, and it will also load the history file (".Rhistory") if there is one. (The history file is a list of commands entered during the previous R session.) If you then change your working directory, to "Rspace" let's say, R will bring along the already loaded workspace and history files, but it WILL NOT load any such files you've stored in "Rspace". Let's say you have the following work habits. You open R, you change to
"Rspace", you do your R business, you quit R and opt to save the workspace. The
next time you start R, your previously created data objects are nowhere to be
seen, even after you change the working directory to "Rspace". Here's how to
retrieve them. First, you may want to get rid of any workspace items R has
dragged along from the default working directory. That can be done with the
rm( ) function. Then you can load the
previously saved workspace (and presumably the history file if so desired) like
this...
If you change the workspace, say by removing "my.data", but then don't save it when you quit or before you change the working directory, "my.data" will still be there when you come back next time. The rm( ) function modifies the workspace, but NOT the workspace image file (".RData"). If you remove "my.data" and save the workspace when you quit, "my.data" will be gone because the new workspace image (.RData file) will overwrite the old one. If you change working directories and then load ".RData" in the new directory, R will ADD it to whatever you've brought with you from the previous working directory. If you don't want that, be sure to clear the old workspace before loading the new one. If your work sessions are long, it's a good idea to save a workspace image
once in awhile, because this image is held in RAM. That means if the power goes
out, it's gone. You can save a workspace image at any time by doing this...
It takes awhile to get used to how the workspace and history files work, when they are saved, when the existing ones are modified, and so on, but it's really quite logical. R is a good old fashioned command line program. It does not do anything that you don't tell it to do. This is one of its BEST features as far as I'm concerned! If you want it to do things automatically upon startup and shutdown, there are scripts you can modify, but that is beyond the scope of this tutorial. Scripts In addition to working interactively with R at the command line, R can also
be scripted. A script is just a text file of R commands (without the command
prompts). For example...
While R has a built-in script editor, I find it handier to use my own text editor, like Notepad (ugh!), vim, or TextWrangler. (The script editor in the Windows version of R is quite good. Pull down the File menu and choose New Script to open it. On the Mac I don't see any reference to a script editor in the menus, and in fact there doesn't appear to be one. If you are running in a terminal, there won't be a script editor. You can copy and paste scripts from any text editor, however.) In this case, I've included this script as its own page at this website. Here's what to do. Click on the link below, right click on the page that appears and choose "Save Page As...", and save the page as a text file to your working directory. (Or save it to the Desktop and move it to your working directory.) Here is the link to the sample script. Once the script is saved in your working directory, simply type...
Saving and Loading the Easy Way The easiest way to save and load data objects you create at the command line
is by using the cryptically named save( ) and
load( ) functions. Any data object--a vector,
a table, a data frame, the output of a statistical procedure--can be saved to
the working directory very simply, as follows...
You don't have to save or load from the working directory either. By specifying a complete path name in the "file=" option, you can put the files anywhere your computer can get to. > rm(y.vector) Reading Files Created Externally As I mentioned in the last tutorial, the most convenient way to create a data
frame is in a spreadsheet program like OpenOffice Calc, iWork Numbers, or
Microsoft Excel. I wouldn't have suggested it if there wasn't some way of reading
those files into R! R will read files created by a very large number of other
applictions, but the easiest way to exchange files with other apps is as plain
text files, and that is what I will discuss here. For details on how to read
other kinds of files, go to the R-project manuals page and read the "R Data
Import/Export" manual:
The best way to keep track of data you are collecting and will be analyzing electronically is to type it into a spreadsheet in the form of a data frame. (ATTENTION my Psyc 497 students: THIS IS REQUIRED!) Just about any modern spreadsheet program will do. If you don't have Microsoft Office Excel, you can go to OpenOffice.org and download Open Office for free. Linux fans can try Gnumeric if Open Office is too clunky. For Mac fans, there is also NeoOffice, an Aqua version of OpenOffice. The following data are from the Handbook of Small Data Sets (Hand et al., 1994), and are from an experiment in which caffeine dose is related to a simple motor task--finger tapping rate.
The following figure shows these data entered into an Excel spreadsheet. Notice I have entered three variables: dose as a factor ("group"), dose as a numerical variable ("dose"), and finger tapping rate in taps per minute ("tapping"). Each variable is entered into its own column, and each column has a variable name at the top in a row of headers. There are no blank rows or fancy formatting, just a row of headers and the data values. Period.
At this point, a decision must be made, which is in what form to save the file. I recommend you save it as an Excel spreadsheet first, but R will not easily read it in that form. (It's possible, but not recommended.) So you will also need to save it in plain text form, and the choices are tab separated data values, or comma separated data values. Each form has its advantages and disadvantages. All things considered and long story short, I prefer the comma separated form, or .csv file. So after you save a copy as an Excel spreadsheet (or whatever program you are using), then save a copy as a .csv file. This is a plain text file that can be examined and modified in a text editor, and which R can read with no problem. (Note: If there are commas in any of your data fields, in a character variable like an address, for example, the csv format will have a problem with this. On the other hand, if there are spaces in any of your data fields, the tab or whitespace separated data format might choke on that. Be careful when you're preparing your data file. Don't use commas, and don't use spaces. R will actually work around both of these problems, but it's just easier to avoid the problem in the first place!) It wouldn't hurt you to create this file yourself, but you can also
download it from this link...
Okay, so you've worked with the data frame, have done some analyses, and
have made some modifications to it. Now you want to write the file back to your
working directory as a csv file that is human readable (as opposed to saving in
binary format using save( ) as we did in the
previous section, which is also possible). The function is write.csv( )...
You can also use the save( ) function to save the "caff" object, but the saved file will not be human readable, and it will not be readable by programs like Excel. Files saved with write.csv( ) can be read by any program that will read .csv files, including most statistical software (like SPSS) and virtually all spreadsheet programs and text editors. Saving and Printing the R Console and Graphics Device The methods for doing this are specific to different operating systems, so
pick yours below. So you'll have a graphic to work with, do this...
Windows To save a console session: 1) Click in the R Console window to bring it to focus, 2) Pull down the File menu and choose Save to File..., 3) Proceed as you would when saving any other file. To print a console session: 1) Click in the R Console window to bring it to focus, 2) Pull down the File menu and choose Print..., 3) Be warned that this prints the entire console session, which can be VERY long. If you want to print just a part of it, highlight that part first, then follow steps 1 and 2. To save a graphic: 1) Click in the Graphics Device window to bring it to focus, 2) Pull down the File menu, choose Save as..., and choose the desired format, 3) Proceed as you would when saving any other file. (Note: If you want to share this graphic with friends who may not be using Windows, DON'T save it as a Metadata file.) To print a graphic: 1) Click in the Graphics Device window to bring it to focus, 2) Pull down the File menu and choose Print... Linux To save or print a console session: There is probably a way to do this, but I have never seen it documented. I highlight what I want to save or print, copy and paste it into a text editor like gedit, and then use that app to save or print. To save a graphic: In an R terminal session, issue the following command...
To print a graphic: Proceed as if you were saving (above) but leave out the file name and "file=" option. See ?dev.print for all the details. Mac OS X To save a console session: 1) Click in the R Console window to bring it to focus, 2) Pull down the File menu and choose Save As..., 3) Proceed as you would when saving any other file. To print a console session: 1) Click in the R Console window to bring it to focus, 2) Pull down the File menu and choose Print..., 3) Be warned that this prints the entire console session, which can be VERY long. I don't know that there is a way, from within R, to print just a part of it. I highlight what I want, copy and paste it to a text editor, and go from there. To save a graphic: 1) Click in the Quartz device window to bring it to focus, 2) Pull down the File menu and choose Save As..., 3) There aren't many options! The file will be saved in pdf format. To print a graphic: 1) Click in the Quartz device window to bring it to focus, 2) Pull down the File menu and choose Print..., 3) You can also print the image to a pdf file this way. All Operating Systems When I say "text editor" in the above notes, I mean text editor, not word processor. If you are copying and pasting from R to a word processor, change the font in the word processor to something like courier new, or some other monospaced (typewriter-like) font. This will keep your tables and so forth aligned properly. revised 2010 August 2 | ||||||||||||||||||||||||||||||||||||