Chapter 1 Getting Started in RStudio

RStudio is what is called an Integrated Development Environment for R. It is dependent on R but separate from it. There are several ways of using R but R Studio is arguably the most popular and convenient. It’s time to open it up and get started! When you first open up RStudio, this is what you should see:

There’s a whole lot of text that (1) isn’t very interesting, and (2) generally isn’t that important so you don’t need to pay much attention to it.

1.1 R as an interactive envrionment

RStudio has four panels: Current file (Editor), Console, Environment, and Viewer.

1

The Information area (all of the right-hand side of RStudio) shows you useful information about the state of your project.

The top panel of the Information side of RStudio contains the Environment and History panes. History contains the history (unsurprising I know) of commands that have been typed into the console. The Environment is virtual storage of all objects you create in R, and contains your list of variables, data frames, strings, etc., which R has been told to save from previous commands. The bottom panel of the Information section contains a number of different panes - Files, Plots, Packages, Help, and Viewer. In other words, you have a file manager (where you can view which files are loaded into your project), a panel to show plots and graphs, a list of packages, help on the functions available within R, and viewer allows you to view local web content.

On the left-hand area of RStudio, you have your Current File (Editor) and Console. The “heart of R” is the Console window. This is where instructions are sent to R, and its responses are given. It’s the place to try things out, but don’t want to save. Finally, the Editor is where you write more complicated scripts without having to run each command. When you run such a script file, it gets interpreted by R in a line by line fashion. This means that your data cleaning, processing, visualization, and analysis needs to be written up in sequence otherwise R won’t be able to make sense of your script. There is an important practical distinction between the Console and the Editor: In the Console, the Enter key runs the command. In the Editor, it just adds a new line.

The hash (#) marks everything to the right of it as comment. Using comments can be very useful for annotating your code, and it improves the readability too. It will also help you to remember why/what you done months later, when you return to your code and you have inevitably forgotten!

1.2 Setting Up Your Working Directory

Step 1. Create a folder (sub-directory) named Rworkshop on your “Desktop” folder

Step 2. From RStudio, use the menu to change your working directory under Session > Set Working Directory > Choose Directory

Step 3. Choose the directory you’ve just created in Step 1

Step 4: Select File -> New File -> R Script. In the window that appears, click on the disk icon just below Untitled1 and save the blank script as session1.R. Make sure that this saves in your Rworkshop folder.

For the purpose of this course, we’re going to be using scripts, but first, lets have a play around with the console so you can familiarise yourself with it.