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.1 Recommended Settings
First of all, let’s set some settings in RStudio.
At the top of the R window, click Tools
, then Global Options
, and do the following:
- On the left side of the window that pops up, make sure it’s on the “General” tab
- Find the “Workspace” section on the right, make the following changes:
- uncheck “Restore .RData into workspace on startup”
- Change the “Save workspace to .RData on exit” option to never
- On the left side, select the “R Markdown” tab and make the following change:
- Change the “Evaluate chunks in directory” option to Project.
- You may need to install the
rmarkdown
package to populate this option. Run the lineinstall.packages("rmarkdown")
and restart RStudio (you can ignore the prompt to install RTools on Windows).
- (Optional) On the left side, select the “Appearance” tab and make the changes:
- (Optional) Change the “Zoom:” setting to increase or decrease the interface text size to fit your screen best.
- (Optional) Change the “Editor theme:” setting to find a color scheme that looks good to you.
- Click “Apply”, then “OK” at the bottom of the window.
Step 2 ensures that each time you open RStudio, there’s no “memory” of anything you may have been doing in R previously.
This is a good option for R beginners to avoid confusion and mistakes.
Step 3 ensures that when you knit R Markdown documents, code chunks will use the project directory as the working directory (more on working directories below).
Changing the zoom can also be done using the shortcuts <control> <shift> +
(to increase size) and <control> -
(to decrease size).
On macOS, the commands are <command> <shift> +
and <command> -
.
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”.
- 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.
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
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.
-
Did your R installation go smoothly? If not, could you troubleshoot the errors or find help online?
-
Does using R remind you of other programs you have experience with?
-
What could be some reasons that using R code written by someone else might not work on your computer?
Any feedback for this section? Click here