9.1 Workspace management functions
Here are some functions helpful for managing your workspace that we’ll go over in this chapter:
Code | Description |
---|---|
ls() |
Display all objects in the current workspace |
rm(a, b, ..) |
Removes the objects a , b … from your workspace |
rm(list = ls()) |
Removes all objects in your workspace |
getwd() |
Returns the current working directory |
setwd(file = "dir) |
Changes the working directory to a specified file location |
list.files() |
Returns the names of all files in the working directory |
write.table(x, file = "mydata.txt", sep) |
writes the object x to a text file called mydata.txt . Define how the columns will be separated with sep (e.g.; sep = "," for a comma–separated file, and sep = \t" for a tab–separated file). |
save(a, b, .., file = "myimage.RData) |
Saves objects a , b , … to myimage.RData |
save.image(file = "myimage.RData") |
Saves all objects in your workspace to myimage.RData |
load(file = "myimage.RData") |
Loads objects in the file myimage.RData |
read.table(file = "mydata.txt", sep, header) |
Reads a text file called mydata.txt , define how columns are separated with sep (e.g. sep = "," for comma-delimited files, and sep = "\t" for tab-delimited files), and whether there is a header column with header = TRUE |
9.1.1 Why object and file management is so important
Your computer is a maze of folders, files, and selfies (see Figure 9.2). Outside of R, when you want to open a specific file, you probably open up an explorer window that allows you to visually search through the folders on your computer. Or, maybe you select recent files, or type the name of the file in a search box to let your computer do the searching for you. While this system usually works for non-programming tasks, it is a no-go for R. Why? Well, the main problem is that all of these methods require you to visually scan your folders and move your mouse to select folders and files that match what you are looking for. When you are programming in R, you need to specify all steps in your analyses in a way that can be easily replicated by others and your future self. This means you can’t just say: “Find this one file I emailed to myself a week ago” or “Look for a file that looks something like experimentAversion3.txt
.” Instead, need to be able to write R code that tells R exactly where to find critical files – either on your computer or on the web.
To make this job easier, R uses working directories.
##The working directory
The working directory is just a file path on your computer that sets the default location of any files you read into R, or save out of R. In other words, a working directory is like a little flag somewhere on your computer which is tied to a specific analysis project. If you ask R to import a dataset from a text file, or save a dataframe as a text file, it will assume that the file is inside of your working directory.
You can only have one working directory active at any given time. The active working directory is called your current working directory.
To see your current working directory, use getwd()
:
# Print my current working directory
getwd()
## [1] "/Users/nathanielphillips/_code/ThePiratesGuideToR"
As you can see, when I run this code, it tells me that my working directory is in a folder on my Desktop called yarrr
. This means that when I try to read new files into R, or write files out of R, it will assume that I want to put them in this folder.
If you want to change your working directory, use the setwd()
function. For example, if I wanted to change my working directory to an existing Dropbox folder called yarrr
, I’d run the following code:
##Projects in RStudio
If you’re using RStudio, you have the option of creating a new R project. A project is simply a working directory designated with a .RProj
file. When you open a project (using File/Open Project in RStudio or by double–clicking on the .Rproj file outside of R), the working directory will automatically be set to the directory that the .RProj
file is located in.
I recommend creating a new R Project whenever you are starting a new research project. Once you’ve created a new R project, you should immediately create folders in the directory which will contain your R code, data files, notes, and other material relevant to your project (you can do this outside of R on your computer, or in the Files window of RStudio). For example, you could create a folder called R
that contains all of your R code, a folder called data
that contains all your data (etc.). In Figure~9.4 you can see how my working directory looks for a project I am working on called ForensicFFT.