3.5 Workspace setup

Whenever you are programming in R, and especially for this class, it’s important to stay organized. This section will give you some instructions and tips for how to organize material for this R course.

3.5.2 Setting working directory

Every time R runs, it has a working directory, which is the folder where R “looks” when loading and saving files. In RStudio, the Files window contains the “More” menu, which has options to set as working directory or go to working directory. This will become more relevant when you start loading data and saving results later in the course. For this course, you’ll be using an RStudio project, which automatically sets the working directory.

See here for more information about working directories.

3.5.3 Create RStudio Project and directories for class

RStudio also has a feature called projects, which is a way of compartmentalizing your R code. This makes it easy to switch between different projects. For this class, you should set up a new project, so all of your project related files are in one place.

3.5.3.1 Create RStudio Project

To create an RStudio project, follow these steps:

  • Click on the “Project” button at the top right of the RStudio window and select “New Project”.
Click this button to create a new project

Figure 3.4: Click this button to create a new project

  • In the window that pops up, click on “New Directory” then “New Project”.
  • In the box after “Directory name”, type “RModule1”, which will be the name of the project.
  • Then click the “Browse” button to select where to place the project.
  • You are free to choose any location on your computer that makes sense to you. It might be most convenient to place it on your desktop for now.
  • Click on “Create Project”.

You should now be in your newly created project. If you look at the Files window in the lower right pane of RStudio, you should see the files in your new project directory, which should only be one file, called “RModule1.rproj”. This file is the project file, which tells RStudio that this directory contains an R Project. When you’re working on this course, you should be working in this project. The easiest way to open up the project is to use your operating system’s file explorer and click on the project file. This will automatically set the working directory to the project directory.

3.5.3.2 Create Directory Structure

To stay organized, you should also create the following folders inside your project directory

  • scripts
  • data_raw
  • data_clean
  • output

You can create these either using your operating system, or the “New Folder” command in the file window within RStudio.

3.5.3.3 Video

Check out this video to watch me set up a project and the new directories.

https://youtu.be/0saBBd6lQDI

3.5.3.4 Set

3.5.4 Some useful commands you should know

As you program in R, you’ll end up creating many different R objects (more on this later), and sometimes you might want to clear all objects in your R environment. This will reduce the amount of memory that is taken up

rm(list = ls())  # Clear everything in your workspace
gc()             # Perform garbage collection
          used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
Ncells  879304 47.0    1676052 89.6         NA  1322238 70.7
Vcells 1630688 12.5    8388608 64.0     102400  2584209 19.8

You might also want to clear the R console, which you can do by placing your cursor in the R console and typing <control> l (careful! that’s a lowercase L).

Here’s a more complete list of RStudio shortcuts.

Before moving on to the next section, take a note of all you’ve done so far.

  1. Did your R installation go smoothly? If not, could you troubleshoot the errors or find help online?

  2. Does using R remind you of other programs you have experience with?

  3. What could be some reasons that using R code written by someone else might not work on your computer?