Saving / Moving R Objects and Packages
If you become a frequent user of R, you may find that you want to get some object that you made in R and move it to a different computer to work on it. Or perhaps you have run out of room on your Stanford account and need to delete your R objects, but would like to save some of them separately and zip them up. This tutorial gives some useful commands for working with R objects. However, this is for manipulating R objects, which are meant to be read and handled in R. This is not how you would export a data.frame so that you could give it to someone to read as a spreadsheet, for example (for which you would want write.table maybe)
Warning: Many parts of this may be Windows specific...
If you just double-click from the desktop, you probably don't know what directory you are using - R starts in the default directory. You can find what directory you are in with the getwd() command:
Note that R uses "/" to separate directories (like unix) and not the Windows' "
". If you are entering a directory path you can use "
" or "/" but not Windows' "
".
If you want to change directories, you can use the setwd() command or if you are in the Windows GUI, you go to "File..." and then click on "Change dir"
R has many commands that allow you to navigate files and directories, like on Unix, only the commands are generally NOT the same. Here's some translation (the Unix commands are not always exactly equivalent, so you should check out the R help):
R | Description | Unix |
list.files/dir | List files in given directory | ls |
dir.create | Makes new directory | mkdir |
unlink | Deletes file or directory | rm |
setwd | Change the working directory | cd |
getwd | Print working directory | pwd |
R Specific | ||
file.remove | file.create | file.exists |
file.rename | file.append | file.copy |
If you use R a lot, you might find that for some projects or classes you would like your objects for that project/class accessible in separate folders. For example, you might want to remember what that data xinc was for before you deleted it or so you don't have memory problems, particularly if you are working off of Leland and thus may run out of space.
However, this is easier to explain if we first learn about saving objects.
Saving in R
If you want to save on particular object that you made (like a dataset) for use in R later, you can use the command save. If you make the extension of your file ''.rdata'' then Windows recognizes it as an R Data file and will "open" it in R by autolaunching R and making the objects saved in the files the available objects in R.
If you exit R and look at the files in your working directory, you will see Numb.Rdata. (If you can't find where it is saved, then you probably just don't know your working directory - use getwd() from above to find out). Now this is a file you can move around to different directories or email, etc. just like any other file on your computer - this is a way of saving your information. (dump() is another way, but not as nice and takes up more space). If you move it to a different machine, say, you can get it back within R by using load()
You can save more than 1 object to a file giving a vector of the character names of the objects using the option list. For example if I made two objects named temp and temp2, then I could do either of the following commands
You can save the entire history of commands using the command savehistory(). By default, these commands are saved to the default file, ".Rhistory", but you can set your own. You can also load a saved history with loadhistory().
See explanation at the end of my Introduction to R Lab (Scroll way down to the part on Plots)
Of course, if you save at the dialog box prompt when you exit R, you don't need to load the information back in, because the object "Numb" and any other objects you made are saved. Except for any plots, everything is as if you just left it. Sometimes you want to backup your entire session in the middle, in case something crashes, etc.
In fact, when you save when exiting, R is actually calling the function save.image, which saves all of the objects to a file ".RData" in your working directory. So you can actually access all of your data that you saved when you exited by loading the .RData file. So you can also call save.image() in the middle of a session to save everything to the .RData file in your working directory rather than waiting to the end.
If you call save.image on its own, the history of commands will not be saved, however. If you save at the prompt, R also calls savehistory() to save the commands and every time you start R, R opens the file ".Rhistory" if it exists, and makes the commands found there your current history. If you wish to truly backup everything, make sure you call savehistory() as well
load(), save.image(), setwd(), savehistory(), loadhistory(), source() can also be called from the R Gui under "File".
Often times, it is useful to have different R sessions for different projects or classes. By this, I mean different working directories with corresponding different ".RData" files. One advantage of this, for example, is when you type ls(), you will only see the objects in that ".RData" file so you only have objects for that project. Similarly, if you save a plot or an object, they will be by default in that directory. This helps to keep your projects separate, and also helps in not accidentally using the wrong data or function!
If you are working off of Leland, or generally a unix machine, where you type "R" will be where the working directory is automatically assumed to be and thus where a ".RData" file is created when you save and exit. So if you are in a folder "stat" for your class, if you type "R" at the prompt, the objects will be there.
If you would like, you can create separate shortcut menus on your desktop to go different folders which accomplishes this task. This is particularly useful if you are frequently using this folder/project
Otherwise, if you have an Robject saved as a "xxx.rdata" file (see below), then double-clicking on that file will open R with the working directory where the file was. This includes the ".Rdata" file that R generates when you save when you exit. So if you don't want a short-cut for every folder you have, you could alternatively:
This will create a ".RData" file for this directory (with only temp in it). When you double-click it, R will open with that working directory. This is clearly a round-about way to do it, but at least it works.
This is fairly basic, and is largely applicable to users of the Windows GUI. Libraries are additional functions that are available in R, usually more specialized. If the library is already on your computer (i.e. it's one of the standard libraries included in R or you've downloaded it) then you can just type:
> library(MASS)
This brings the library up so you can access its functions. You can find out what is included in the package with:
> help(package=MASS) > data(package=MASS)
At Personal Computer (Windows) - using the example of the package ISwR:
> library(ISwR)
You can also do all of this by command line with commands like download.packages and install.packages, etc, as is done a little in the Troubleshooting below. Generally downloading the package manually by going in your browser to the CRAN page, downloading it, and installing it from your download can be tricky in Windows, because it will not be built correctly or the right zip file etc. Use the equivalent commands provided by R if at all possible.
Troubleshooting:
> install.package(c("ISwR","gregmisc"),lib="C:/Documents and Settings/joesmith/My Documents/RLib")
> library(ISwR,lib.loc="C:/Documents and Settings/joesmith/My Documents/RLib")