R Studio: Understanding Our Environment
When you open RStudio, you’ll see the following interface:
1. Code Editor
This is the area in the interface that you can write lines of your code. To create a markdown navigate to: File -> New File -> Notebook. The notebook will allow you to create code blocks that you can save and revise later. You can run sections of code (similar to the way Jupyter works for python). For large projects, this is the where you’ll store and run your scripts as it is easy to edit, store and revisit.
2. The Console
The console is the place in your RStudio that actually executes the commands. Scripts that are run in the code editor will be sent to and executed in the console. The commands you feed to the console are stored and remembered only for that session. In explanation, when you close RStudio, you won’t be able to see the commands from your previous session. While you can run code in the console, it is a little more difficult to edit and re-run in this environment. I prefer using the code editor for scripting. Some scripts that wouldn’t require editing (e.g. installing and loading libraries) can be executed in the console.
3. Output Visualization, File Manager, Library Manager, and Help Desk
This quadrant wears a lot of hats.
Plots: You can visualize the output of each code block you run in the console. When the console executes a script that has a visual output, it sends it to the output window where it is displayed.
Files: You can manage your file system by choosing the Files tab on the menu. This can be useful if you want to check which directory a file is stored without using code.
Packages: When you install a package, you can see it updated in this tab. You can reference the packages tab to check if you have already installed a package or not.
Help: This is an incredibly useful resource on RStudio! You can search for any command or package in this tab to read its manual. You can get an understanding of the syntax or argument(s) required to use the command.
Your workspace shows you what values you have available. When you define a new variable (e.g. array or table), it will pop up in the environment. You can inspect the variable from this environment. Navigating to the history tab on the environment will show you the last commands that were run.
Installing Packages and the tidyverse
To begin using R, you will need to install some R packages. A package is a collection of functions, data, and documentation that extends the capabilities of base R. Using packages is key to the successful use of R. The majority of the packages that you will learn in this book are part of the so-called tidyverse. The packages in the tidyverse share a common philosophy of data and R programming, and are designed to work together naturally.
You can install the complete tidyverse with a single line of code.
Try it out: In this module, you’ll see text boxes like this one. You can copy and paste the text in the boxes in your own R Studio. Paste the code below in your Console window.
Once the packages are downloaded, your prompt would return. Check your packages in the right bottom window to ensure that the download is complete. R will download the packages from CRAN and install them on to your computer.
After installing a package, you must load its functions to your current workspace using the
library()function. Paste this in your console window as well:
As each package downloads, the status will return in your console. The name of each package followed by a checkmark let’s you know that the download was successful. This tells you that tidyverse is loading the ggplot2, tibble, tidyr, readr, purrr, and dplyr packages. These are considered to be the core of the tidyverse because you’ll use them in almost every analysis.
You will only need to install packages the first time. From then on, you would simply need to load in the corresponding library. To understand the functions and objects of a package, you can use the help function:
Running some code
To gain some familiarity with RStudio, let’s type some simple code and run it. To save and view your progress, it is best to create a new notebook and write snippets of code there. To navigate to the notebook go to: File > New File > New Notebook. R provides some guidelines to get you started with a notebook. You will notice that there is a slightly darker shaded box, labelled r:
As you’d expect, this box accepts code that is written in r. To run a line of code in the box, click the green arrow on the top right corner of the box. Let’s start with something simple.
>1 + 2
The next line should return the output.
Some RStudio Assistance:
Type this code and notice you get assistance with the paired quotation marks:
x <- "hello world"
Quotation marks and parentheses must always come in a pair. RStudio does its best to help you, but it’s still possible to mess up and end up with a mismatch. If this happens, R will return an error that let’s you know that it is looking for something more.
x <- "hello
When typing in a function, RStudio tends to gives you assistance to fill in what you’re looking for. For example, if you are looking for the
seq() function, type se and hit TAB. A popup shows you possible completions. Specify seq() by typing more (a “q”) to disambiguate, or by using ↑/↓ arrows to select.
A floating tooltip pops up, reminding you of the function’s arguments and purpose. If you want more help, press F1 to get all the details in the help tab in the lower right pane.
Press TAB once more when you’ve selected the function you want. RStudio will add matching opening
) parentheses for you.