Setup
In this section we cover all of the start-up activities that you need to complete in order to use RStudio effectively: install the software, prepare your computer to hold the data that you will be using, load the tidysphere
library, and download data that you will use RStudio to analyze.
1 Installing software
1.1 Install R and RStudio
Tons of resources on the Internet, both video and written, can help you install this (free) software, whether it be on a Windows, Mac, or Linux computer.
1.3 Installing needed libraries: tidyverse
and tidylog
The tidyverse
is a collection of packages (R code and tools) for data analysis. The tidylog
library makes warning and status messages more helpful. You will be using these throughout this course.
- Use the
Console
, found in the lower left window after you launch RStudio. - Type
install.packages("tidyverse")
and then hitEnter
. - After the above is done (and it will take a while), type
install.packages("tidylog")
and then hitEnter
.
Installation takes a little while; just let it finish execution before continuing. This only needs to be done once on each computer.
1.4 Project Organization
One advantage of automating work with a script is that you can reuse it later. To do that, it’s helpful to have work organized so it’s easy to find in an organized folder structure.
2 Set your RStudio preferences
While working with R and RStudio, we have learned some things about how to set up RStudio that will help you enjoy your experience and be more productive.
Go through the following steps in order to accelerate your enjoyment and productivity:
- Open
Tools/Global options
in order to set your preferences. - Set the following under the
General
/Basic
tab:- So that you can find scripts that you create when not in a project, you can set your
Default working directory
to a top-level folder like the one described above. Note that, generally, you will be working in a project so R will never use this directory directly; however, set this just in case you create some files outside of the scope of a project. This is one of those “better be safe than sorry” situations. - When you start an R session, you can set it up so that RStudio automatically loads all that you did in your previous session. We recommend that you not do this! Why? Because it can store a lot of information, and you will lose track of exactly what it is that you have done. Better to have a bit of inconvenience while maintaining some control over your work environment. To ensure that you begin each day with an empty session, do the following:
- Uncheck
Restore .RData into workspace at startup
and - Change
Save workspace to .RData on exit
toNever
.
- Uncheck
- Keep all the other options the same under
General
. - Click the
Apply
button.
- So that you can find scripts that you create when not in a project, you can set your
- Under
Code
/Editing
:- Make sure that Use native pipe operator is checked.
- Click the
Apply
button. - The default keyboard shortcut for the pipe is Shift+Ctrl/Cmd+M, which takes three fingers. You may prefer to pick an easier combination like Alt+M. To do that, under
Code
/Editing
look for thekeyboard shortcuts
button, and use it to filter to the pipe command (type “pipe” in the filter) and change the key binding as you like.
- To change how RStudio looks (simply for your own work style), change any settings in
Appearance
(a left-menu button in this dialog box). Click theApply
button when done. - For working with scripts (which you will be doing soon enough), you should set the following preference. Go to the
R Markdown
/Basic
tab.- Look for the
Show markdown preview in
option. - Change it to
Viewer pane
. (If you have a large screen that you work on, you might change this toWindow
; however, on a laptop, it’s generally more convenient to use the Viewer pane.)
- Look for the
- Click the
OK
button.
As you gain experience with RStudio, you will want to return to your settings throughout the Global options
dialog box and see if you want to change anything. The defaults are perfectly reasonable but these options are here for a reason.
3 Starting a new project
The following are the steps that you should complete when starting a new project. You might consider printing out this page as a reminder of the steps to take while you are at the beginning of your learning journey.
3.1 Preparing your computer for data
When you are doing any data analysis project, you have to have a place to put the data (we did that above) and tell RStudio where to look for data (which we’ll do now).
3.1.1 Create a new project subdirectory
Any time that you start a new project, create a folder for it within your overall folder structure. There you will store the data, scripts, and output related to the project. For all but the smallest projects, you may want to set up the project folder with subfolders called /R
(for R scripts), /data
(for data files), and output (for whatever gets generated). You may also want a /docs
subfolder to keep documentation like notes and presentations in.
3.1.2 Create a new RStudio project
After creating the subdirectory, you need to tell RStudio that you are beginning a new project. You do this with the menu choice File/New Project...
; when the dialog box comes up, choose Existing Directory
and then browse to the directory that you just created.
The wonderful thing about creating this project is that now RStudio knows where to look for data—i.e., where the working directory has been set. The project will also remember which files you have open, so you can jump right back in after closing RStudio and returning to the work later on.
If you want to know what the working directory is for R, then go to the Console
and type getwd()
.
4 Returning to an existing project
After you have gone through all of the pain of starting a new project, subsequent working sessions will be much easier. When you start RStudio, it will automatically go to the most recent project. If you are starting up RStudio after working on some other project or with a completely empty RStudio workspace, then use the menu to fine File/Recent Projects
and choose the name of the project that you want to work on. The project will open, the working directory will change, and the files that you most recently were working on will be open. There’s also a project shortcut menu top right of the RStudio window.