Chapter 2 Introduction

In this chapter I will offer description of R, RStudio, and R Markdown. These are the software/progams you will need throughout the manual.

2.1 Install R and RStudio

R is an open source statistical package that is free and platform independent. R can be downloaded for any platform The Comprehensive R Archive Network (CRAN). After clicking the link, choose the appropriate (Linux, (Mac) OS X, or Windows) installation file for R. Download and install R on your computer. I strongly recommend the use of RStudio as opposed to the R GuI to do your work in R. RStudio is an integrated development environment (IDE) for R. It includes a console, syntax-highlighting editor that supports code execution, as well as tools for plotting, history, debugging, workspace management, and report writing. Like R, RStudio is open source and free to download and use. Following the link RStudio Desktop. Choose the appropriate version for your environment. In addition, since you will need to write reports, R Markdown gives you the ability to integrate documents with your code to produce outputs in a variety of formats.

Soren L. Kristiansen has written comprehensive guides to installing all of the above files for both the macOS and Windows. Please follow the appropriate link to ensure you have installed everything properly on your machine. Follow the steps exactly and you will have no issues. Don’t follow them at your own peril.

In addition these guides will walk you through the creation of your first R Markdown file. You are expected to use R Markdown for your work. This guide was written using R Markdown.

RStudio also includes a code editor which allows you to maintain a file of your ‘scripts’ as you complete your code. This script file also allows for relatively easy editing and debugging of your code as you write it.

2.2 Using RStudio

RStudio contains 4 panes that make elements of R easier to work with than they would be working in R GUI.

2.2.1 Source Pane

The Source Pane in the upper left contains your code and can be accessed with the keyboard shortcut CTR+1. This pain includes any R Markdown files, R Notebook files, and R Script files that you may have opened. It will also contain any data that you view or information about attributes of an object when clicked in the environment pane.

This is the pane in which you would create or open a script file. Create a new script file by clicking the icon with the green “+” and clicking R Script, by clicking File > New File > R Script, or with Ctrl+Shift+n or cmd+Shift+n.

2.2.2 Environment/History Pane

You’ll find the Environment/History pane in the top right. The Environment tab shows you the names of all the data objects you have defined in the current R session. You can directly access the environment with ctrl+8. This tab is objects() command mentioned in the text on steroids. By clicking on the triangle icon next the object name you receive the same information as calling str() on the object. Clicking on the grid icon the right of the name will call view() and the object will be displayed in the Source Pane. view will display all of the data in a data frame. head displays the first six observations. The History tab contains all the code you that you’ve run. You can directly access the history tab with ctrl+4. If you’d like to re-use a line from your history, click the To Console icon to send the command to the Console Pane, or the To Source icon to send the command to the Source Pane.

2.2.3 Console Pane

The Console Pane in the bottom left is essentially what you would see if you were using the R GUI without R Studio. It is where the code is evaluated. You can type code directly into the console and get an immediate response. You can access the Console Pane directly with ctrl+2. With your cursor in the Console you can access any previous code with ctrl+up and use the arrow keys to pick the line you’d like to use. Using the up arrow will show the lines of code one at a time from the last line ran to the first line available in the R session. If you type the first letter of the command followed by ctrl+up you will get all of the commands that you have used that begin with that letter. Highlight the command and press return to place the command at the prompt.

2.2.4 Files/Plots/Packages/Help Pane

The last pane is on the bottom right. The files tab (ctrl+5) will show you the files in the current working directory. The plots tab (ctrl+6) will contain any plots that you have generated with the base R plotting commands. The packages tab (ctrl+7) will show you all of the packages you have installed with checks next to the ones you have loaded. Packages are collections of commands that perform specific tasks and are one of the great benefits of being part of the R community. Finally, the help tab (ctrl+3) will allow you get help on any command in R, similarly to ?commandname. Initially understanding R help files can be difficult; follow the link A little more about R from Kieran Healy’s Data Visualization for a good introduction. In addition args(commandname) displays the argument names and corresponding default values (if any) for any command.

Double clicking a csv file in the Files tab will open the data in the Source Pane.

Find a nice overview of R Studio in YaRrr! The Pirate’s Guide to R

2.3 R Markdown

You will complete your homework, reports, etc. using R Markdown gives you the ability to integrate documents with your code to produce outputs in a variety of formats.

R Markdown allows you to combine R code with your report for seamless integration. R code can be included in a markdown file as Code Chunks or directly within the text.

To create an new R Markdown file inside of RStudio by clicking File > New File > R Markdown... A dialog box will appear choose a title that is descriptive of the work you are doing, click OK. This will create a default R Markdown. The first thing it creates is yaml header. The header includes the title, author, date, and default output file type. You will want to retain this. It will also generate R code chunk with knitr1 options. You will want to retain this R chunk. You need not retain any of the remaining parts of the file generated.

Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

Scroll down to the Overview at for more on markdown. I strongly suggest you work through the Markdown formatting tutorial for an introduction to basic formatting in Markdown.

For more help getting started in R Markdown, please see the R Markdown website.

  1. knitr is an engine for dynamic report generating within R.