R Projects are a super helpful feature of RStudio. They help you:

Let’s go over how to create and use an R Project!

New R Project

Let’s make an R Project so we can stay organized in the next steps. Click the new R Project button at the top left of RStudio:

The New R Project button is highlighted.

In the New Project Wizard, click “New Directory”:

In the New Project Wizard, the 'New Directory' option is highlighted.

Click “New Project”:

In the New Project Wizard, the 'New Project' option is highlighted.

Type in a name for your new folder.

Store it somewhere easy to find, such as your Desktop:

In the New Project Wizard, the new project has been given a name and is going to be stored in the Desktop directory. The 'Create Project' button is highlighted.

You now have a new R Project folder on your Desktop!

Make sure you add any scripts or data files to this folder as you go through your Intro to R lessons, or work on a new project. This will make sure R is able to “find” your files.

The image shows an image of an arrow pointing to the newly created R project repository.

Testing out your R Project

Let’s read in some data.

read_csv() needs an argument file =.

# Examples

dat <- read_csv(file = "www.someurl.com/table1.csv")

dat <- read_csv(file = "/Users/avahoffman/Downloads/Youth_Tobacco_Survey_YTS_Data.csv")

dat <- read_csv(file = "Youth_Tobacco_Survey_YTS_Data.csv")

If we aren’t reading from URL, and we are Reading from your computer.. What is the “path”?

GIF with text. PC: *autosaves file* Me: Cool, so where did the file save? PC: shows image of Power Rangers shrugging.

When you set up an R Project, R looks for files in that folder.

Download the data file at http://jhudatascience.org/intro_to_r/data/Youth_Tobacco_Survey_YTS_Data.csv. Move downloaded files into the R Project folder.

Image showing the csv dataset being moved to the R Project directory created earlier.

Confirm the data is in the R Project folder.

Image showing the csv dataset inside the R Project directory created earlier.

If we add the Youth_Tobacco_Survey_YTS_Data.csv file to the R Project folder, we only need to use the file name for the file argument:

dat <- read_csv(file = "Youth_Tobacco_Survey_YTS_Data.csv")

NICE!

Why does this work?

When we create an R Project, we establish the working directory.

Working directory is a folder (directory) that RStudio assumes “you are working in”.

It’s where R looks for files.

The files are in the computer text overlaid on still shot of the movie Zoolander.

The working directory is wherever the .Rproj file is.

Image showing the RStudio console. There is an arrow pointing to the .Rproj file. The top right corner shows that the 'Intro_to_r' project has been selected.

TROUBLESHOOTING

If your R project directory and working directory do not match:

Screenshot of the session menu, with set working directory selected, and To Project Directory selected.

If you are trying to knit your work, it might help to set the knit directory to the “Current Working Directory”:

Screenshot of the Knit menu, with Knit directory open, and Current Working Directory selected.

You can also run the getwd() function to determine your working directory.

# Get the working directory
getwd()

You can also set the working directory manually with the setwd() function:

# set the working directory
setwd("/Users/avahoffman/Desktop")

here package

the here() function from the here package gives you the absolute path of the R Project file. It can be useful for being very explicit about what the path is.

library(here)
here()