Lesson 5. Create a project & working directory setup

Set up your working directory

As we learned in the previous tutorial, project organization is integral to efficient research. In this tutorial, we will create the project directory that we will use for all of our work. This project directory will be carefully organized with a \data directory that we will use to save all of the data we use in our lessons.

Learning objectives

At the end of this activity, you will be able to:

  • Create an easy to use and well structured project structure
  • Set a working directory in R using code
  • Set a working directory using the RStudio interface

What you need

You will need the most current version of R and, preferably, RStudio loaded on your computer to complete this tutorial.

Create earth-analytics project directory

Now that we have the basics of good project structure out of the way, let’s get our project set up. We are going to create an earth-analytics project directory (or folder) where we will store data and files used in the class. We will then set that project directory as our working directory in R.

Data tip: Directory vs Folder: You can think of a directory as a folder. However the term directory considers the relationship between that folder and the folders within it and around it (it’s full path).

Follow the steps below to create an earth-analytics project directory on your computer and then a data directory located within that project directory.

  • Navigate to the Documents directory on your computer.
  • In the directory, create a NEW DIRECTORY called earth-analytics.

Data tip: Notice that we are creating a easy to read directory name. The name has no spaces and uses all lower case to support machine reading down the road. Sometimes this format of naming using dashes is referred to as a slug.

  • Next, open the earth-analytics directory and create a directory within it called data

We will use the data directory to store the data that we download to use in this course and in the tutorials hosted on this website.

working directory screenshot
Your project directory should look like this. Right now it just contains one directory called data.
  • The final step is optional but recommended - especially if you are new to R and RStudio. Open up RStudio and set your default working directory to the earth-analytics directory that we just created. In RStudio go to: Tools –> Global Options –> Click on the General setting at the top of the global options panel` (see screen shot below).
  • Browse to the earth-analytics directory and set it as your default working directory.
setup working directory in rstudio
Set your default working directory in RStudio to the Earth Analytics directory. That way, every time you open `RStudio` it will default to that directory. Image: RStudio Version 0.99.903.

When you set a default working directory, every time you open RStudio it will default to that working directory being set. This can be nice if you are going to always work in the same directory (like we will in all of our tutorials).

Finally, let’s see what your main working directory looks like. Use the getwd() function to find out what your current working directory is in R.

# view working directory

[1] "/Users/lewa8222/Documents/earth-analytics"

If your working directory path does not match the location where you created your earth-analytics directory on your computer, then we need to fix it. We can set the working directory with R code OR we can use the RStudio interface to set the working directory.

Set working directory in RStudio interface

Let’s set the working directory using the RStudio interface.

  • In the RStudio interface, look at the pane in the LOWER LEFT hand corner of your screen. It should have a tab called Files which opens the files window. In the files window, navigate to your earth-analytics directory which should be within the Documents directory.

Your window should look like the screen shot below:

working directory screenshot
Your working directory should look like this. It should contain just a `data` directory. Image: RStudio Version 0.99.903. Source: Earth Lab.
  • Next, click on the More drop down. Choose Set as working directory
rstudio working directory set
You can set your working directory in RStudio directly. Image: RStudio Version 0.99.903. Source: Earth Lab.

Set working directory using code

We can set the working directory using code in R too. You don’t have to do this if you already set the working directory above. However, it’s good to know how to do it - particularly if you get into more advanced scripting in R. We use the setwd() function to set a new working directory as follows:

# set working directory - MAC File Structure - backslashes

# a windows machine uses front slashes. There is normally a
# drive letter like C:\

All done!

Great work! You are now ready to start working with RStudio!

Leave a Comment