Skip to main content

Introduction to R Projects

The Beginner’s Guide to R Projects

One of the ways people organize their work in R is through the use of R Projects, a built in functionality of R Studio that helps to keep all your related files together. R Studio provides a great guide on how to use Projects so definitely check that out!

What is an R Project?

When you make a Project, it creates a folder where all files will be kept, which is helpful for organizing yourself and keeping multiple projects separate from each other. When you re-open a project, R Studio remembers what files were open and will restore the work environment as if you had never left - which is very helpful when you are starting back up on a project after some time off! Functionally, creating a Project in R will create a new folder and assign that as the working directory so that all files generated will be assigned to the same directory.

What are the benefits to using Projects?

The main benefit of using Projects is that it starts the organization process off right! It creates a folder for you and now you have a place to store all of your input data, your code, and the output of your code. Everything you are working on within a Project is self-contained; which often means finding things is much easier - there’s only one place to look!

Also, since everything related to one project is all in the same place, it is much easier to share your work with others - either by directly sharing the folder/files, or by associating it with version control software. We’ll talk more about linking Projects in R with version control systems in a future lesson entirely dedicated to the topic!

Finally, since R Studio remembers what documents you had open when you closed the session, it is easier to pick a project up after a break - everything is set-up just as you left it!


IF you haven't watched the lesson on R Markdown, Please Watch!

Creating a Project

There are three ways to make a Project:

1) From scratch - this will create a new directory for all your files to go in
2) From an existing folder - this will link an existing directory with RStudio
3) From version control - this will “clone” an existing project onto your computer (Don’t worry too much about this one, you’ll get more familiar with it in the next few lessons)

Let’s create a Project from scratch, which is often what you will be doing!

Open R Studio, and under File, select “New Project”. You can also create a new Project by using the Projects toolbar and selecting “New Project” in the drop down menu, or there is a “New Project” shortcut in the toolbar.

Ways to initiate a new project

Since we are starting from scratch, select “New Project” and a window will appear. Select “New Directory” and when prompted about the Project type, select “New Project”

New project options

Pick a name for your project and for this time, save it to your Desktop. This will create a folder on your Desktop where all of the files associated with this Project will be kept. Click “Create Project.”

Creating a new project

A blank R Studio session should open.

Your new project

A few things to note:

1) In the “Files” quadrant of the screen, you can see that R Studio has made this new directory your working directory and generated a single file with the extension “.Rproj”
2) In the upper-right of the window, there is a Projects toolbar that states the name of your current Project and has a drop down menu with a few different options that we’ll talk about in a second.

Note the new project file in the Files quadrant and the Project toolbar

Opening a project

Opening an existing Project is as simple as double clicking the .Rproj file on your computer. You can accomplish the same from within R Studio by opening R Studio and going to File > Open Project. You can also use the Project toolbar and open the drop down menu and select “Open Project…”

Ways to open a project

Quitting a project or switching to another

Quitting a project is as simple as closing your R Studio window. You can also go to File > Close Project, and this will do the same. Finally, you can use the Project toolbar by clicking on the drop down menu and choosing “Close Project”.

Ways to quit a project

All of these options will quit a Project and doing so will cause R Studio to write which documents are currently open (so they can be restored when you start back up again) and it then closes the R session. When you set up your Project, you can tell it to save environment (so, for example, all of your variables and data tables will be preloaded when you reopen the project), but this is not the default behavior.

The Projects toolbar is also an easy way to switch between Projects - click on the drop down menu and choose “Open Project” and find your new Project you want to open - this will save the current Project, close it, and then open the new Project within the same window. If you want multiple Projects open at the same time, do the same but instead select “Open Project in New Session”. This can also be accomplished through the File menu, where those same options are available.

Ways to switch between projects

Best practices

When you are setting up a project, it can be helpful to start out creating a few directories. Try a few strategies and see what works best for you, but most file structures are set-up around having a directory containing the raw data, a directory that you keep scripts/R files in, and a directory for the output of your code.

For example:

An example of a possible folder structure to organize your project

If you set up these folders before you start, it can save you organizational headaches later on in a project when you can’t quite remember where something is!

Summary

In this lesson we’ve covered what Projects in R are, why you might want to use them, how to open, close, or switch between projects, and some best practices to best set you up for organizing yourself!

Comments

Popular posts from this blog

Mastering Data Science Experimental Design: From Hypothesis to Results

The Beginner’s Guide to Data Science Experimental Design: Now that we’ve looked at the different types of data science questions, we are going to spend some time looking at experimental design concepts, in our last lesson of our first Course "The Data Science Toolbox" in our Data Science Specialization using R Programming. As a data scientist, you are a  scientist  and as such, need to have the ability to design proper experiments to best answer your data science questions! Previous lesson, if you haven't watched! What does experimental design mean? Experimental design is organizing an experiment so that you have the correct data (and enough of it!) to clearly and effectively answer your data science question. This process involves clearly formulating your question in advance of any data collection, designing the best set-up possible to gather the data to answer your question, identifying problems or sources of error in your design, and only then, collecting the app...

The Evolution of R: From S-Inspired Language to Statistical Powerhouse

The Beginner’s Guide to the History of R Programming: R Programming is basically the dialect of S Programming. S History: The S programming language was first developed in the late 1970s by John Chambers and his colleagues at Bell Laboratories. It was initially used for data analysis and graphics, and it served as the basis for the commercial software package S-PLUS, which was released in the early 1990s. While S-PLUS was popular in the statistical community for many years, it has since been largely replaced by the open-source software environment R, which was inspired by S and developed by some of the same people who worked on S-PLUS. As for the current version of S, it's not as widely used as R, and there are several different implementations of the S language that are still available today, including: S-PLUS: This is the commercial implementation of S that was developed by TIBCO Software Inc. It is still in use today, although it has been largely supplanted by R in the statisti...

Streamlining Your Workflow: Linking Git/GitHub with R Studio for Efficient Version Control

The Beginner’s Guide Linking Git/GitHub with R Studio: Now that we have both R Studio and Git set-up on your computer and a GitHub account, it’s time to link them together so that you can maximize the benefits of using R Studio in your version control pipelines. First we will link R studio and Git and then we will link R Studio and GitHub. We will also link an existing Project with Git and GitHub. Linking R Studio and Git In R Studio, go to Tools > Global Options > Git/SVN Use the Global Options menu to tell R Studio you are using Git as your version control system Sometimes the default path to the Git executable is not correct. Confirm that git.exe resides in the directory that R Studio has specified; if not, change the directory to the correct path. Otherwise, click OK or Apply. Confirm that the directory R Studio points to for the Git executable is correct R Studio and Git are now linked. Linking R Studio and GitHub In that same R Studio option window, clic...