Getting Started with R and R Studio

Overview

Instructions and links for downloading and installing the R Studio suite of awesomeness. Includes a brief walkthrough of R Studio

  1. R – The programming language.
  2. R Studio – A fantastic interface for coding in R.
  3. LaTeX – Required to create PDF reports.
  4. R Commander – A true to form GUI (point and click) for using R. (Optional)

Program Installation

Windows

Double clicking on these will start the standard installation process.

Video walkthrough

Macintosh

Video walkthrough

Optional: Install via Homebrew. Guide courtesy of Justin Bankes (2017).

R Studio - A tour

Initial Start

When you first (like very first time) open R studio you will see three panels.

Console

The console is the full panel on the left

  • Everytime you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running.
  • Below that information is the prompt, > . As its name suggests, this prompt is really a request, a request for a command.
  • Initially, interacting with R is all about typing commands and interpreting the output.
  • These commands and their syntax have evolved over decades (literally) and now provide what many users feel is a fairly natural way to access data and organize, describe, and invoke statistical computations.

The console is where you type commands and have them immediately performed.

Environment

The panel in the upper right contains your workspace (aka Environment)

  • This shows you a list of objects/variables that R has saved.
  • For example here a value of 3 has been assigned to the object a.

History

Up here there is an additional tab to see the history of the commands that you’ve previously entered.

Files

The files tab allows you to open code/script files within R studio.

Plots

Any plots that you generate will show up in the panel in the lower right corner.

Getting Help

To check the syntax of any function in R, type ? in front of the function name to pull up the help file.

For example here I typed ?mean to get the help file for the mean function. Admittedly these aren’t the most helpful of files at times. This is where Google / stack overflow / R help list serves and R user groups can be your friend.

Script file (a.k.a code file)

  • Most often your R studio window will have 4 panels.
  • The top left is your editor window, where you write code or script, the console is now at the bottom. Sometimes it’s minimized, you can use the boxes in the top right corner of the console window to bring up the console.

When we type R code in a specific way up here, the results show up both right below the code you just typed, and in the console itself.

2+2

Setting Preferences to retain sanity

R Studio Preferences

To avoid some of the most common errors when learning R let’s set some preferences in RStudio.

  1. On your computer, create a folder for your specific course in an easy to locate place.
    • Recommended paths are short, such as C:/School/R or ~/rdonatello/School/MATH315
    • You will store all files associated with this class here.
    • Do not use any spaces in the folder name, and keep it simple.

Finding your path on a Mac

  1. Select the file or folder in the OS X Finder, then hit Command+i to summon Get Info
  2. Get Info can also be accessed by the control-click and right-click menus.
  3. Click and drag alongside “Where” to select the path, then hit Command+C to copy the full path to the clipboard
  4. Paste this into R for your path.
  5. Make sure all slashes are forward slashes /.

Mac Reference link: http://bit.ly/1NxF9eu

Finding your path on Windows

  1. Open Windows Explorer and browse to the folder or file of your choice.
  2. The path to the folder/file will be indicated in the Address bar of Windows Explorer.
  3. To copy it, simply right-click on the Address bar and select Copy address as text.
  4. Paste this into R for your path.
  5. Make sure all slashes are forward slashes /.

Windows Reference link: http://bit.ly/1NxFeir

R Studio Preferences

Continuing with the R Studio preferences,

  1. In RStudio the file menu go to Tools then Global Options.
  2. Uncheck “Restore .RData into workspace at startup” and “Always save history”
  3. Where it says ‘Save workspace to .RData on exit:” Select ‘Never’
  4. Click apply then ok.

R packages: How things get done.

Installing R Packages

Packages are sets of predefined code, functions and data sets. They provide additional functionality and goodies beyond what is included when you download Base R.

  • Packages are only installed once per computer (until you update R).

  • To install the knitr package type the following command at the prompt > and hit enter to submit the command.

install.packages("knitr")
  • Repeat the above to install the following packages, remembering R is case sensitive: rmarkdown, kableExtra, gridExtra, tidyverse, knitr, nycflights13 and hflights.

Installing R Packages - Shortcut

You can install multiple packages at once by combining the list of package names using the collection operator c().

install.packages(c("rmarkdown", "tidyverse", "gridExtra", "kableExtra",
      "knitr", "nycflights13","hflights"))

LaTeX - professional reporting

Make nice PDF reports and presentations.

LaTeX allows you to turn your reports into a nicely formatted PDF document.

  • Install once and forget about it.

Download from: https://www.latex-project.org/get/

  • Windows: I have had best luck with proTeXt
  • Suck it up and get the 2G download for less headache later. (Don’t do this on the campus wifi during class time.)

You can edit *tex files directly in R Studio, a separate tex editor is not necessary.

R Commander: Pointing and clicking your way along

A Basic Statistics GUI for R

Additional setup for R Commander

  • Install via the Rcmdr package.
  • Website: http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/
  • You will need to install Pandoc converter engine if you want to create PDF output as Rcmdr uses the old markdown package which only can create HTML output.

More help

For more help and/or places to start learning, I would recommend starting with the resources R Studio already has curated for you. https://www.rstudio.com/online-learning/

Related