Introduction to R

Introduction to R

R is a free, open-source software environment for statistical computing and graphics. It is able to compile and run across a wide variety of UNIX platforms, Windows, and MacOS. If after running through the examples today, you would like to learn more about R or complete additional examples, you can download and install R on your own computer.

Installation

The latest version of R (4.1.0 Camp Pontanezen) is available for download and installation on your personal machines.

Installing R

Windows

  1. Go to https://cloud.r-project.org/bin/windows/base/
  2. Click the “Download R 4.1.0 for Windows” link.
  3. When the file finishes downloading, double-click to install, following the instructions in the dialog.

Mac

  1. Go to https://cloud.r-project.org/bin/macosx/
  2. Click the “R-4.1.0.pkg” link
  3. When the file finishes downloading, double-click to install. You should be able to click “Next” to all dialogs to finish the installation.

RStudio

RStudio is an integrated development environment (IDE) for R. The RStudio IDE is free and open-source that works across Windows, Mac, and Linux platforms.

The examples will be described and made available using R Markdown (integrates Markdown, a simple formatting syntax for web pages, with R scripts) and Sweave (integrates LaTeX, a document formatting syntax, with R scripts). R Studio allows easy use and creation of these documents, but requires the knitr R package.

Installing RStudio

RStudio can be downloaded and installed on a Mac, Windows, or Linux machine using the directions provided.

  1. Go to https://rstudio.com/products/rstudio/download/#download
  2. Select the appropriate installer for your operating system:
  3. When the file finishes downloading, double-click to install. You should be able to click “Next” to all dialogs to finish the installation.

Using RStudio

R is the base statistical computing environment. RStudio is an “Interactive Development Environment” makes it easy to use R. It does things like auto-complete, syntax highlighting, and is generally much easier to use.

After you install R and RStudio, you only need to run RStudio.

The initial view of RStudio should look similar to the following (note, the images used in this demo are old and therefore have an older version of R listed):
RStudio-start-screen

RStudio-open-with-file

RStudio-open-with-file

The RStudio environment is made of several panels. By default there are four panes: Script Editor (top left), Console (bottom left), and the panes with multiple tabs on the right.

Console Panel

The console panel is where you can quickly and immediately execute R code. You can experiment with functions here, or quickly print data for viewing.

RStudio-Console (note, older version of R used)

RStudio-Console (note, older version of R used)

Console Practice

You can try typing 2+5 into the console to see the result in R.

2+5
## [1] 7

Scripting Panel (Script Editor)

The scripting panel is where R scripts (or R Markdown, or R Sweave documents) can be edited. When using R Markdown or Sweave, the files consist of a combination of text (including marked up notation or latex commands) and code. If editing a .R file, then the document is just of code.

When editing a RMarkdown or Sweave document, chunks of code can be run using controls at the upper-right of each code section (play button, etc.)

RStudio-scripting-panel)

RStudio-scripting-panel)

Other Panes

The Environment tab displays the variables, functions, data frames and other objects that are in the current workspace. Data can be imported from several other data formats, or cleared.

Rstudio-environment-tab

Rstudio-environment-tab

For data loaded into the workspace, it can be selected (click on iris) to view the data set.

RStudio-data-view

RStudio-data-view

The History tab list all commands recently used of associated with a project.

The Files tab lists the files located in the current working directory.
RStudio-files-panel

The Plots tab is where graphical output will be displayed

The Packages tab shows currently installed libraries/packages and which are loaded for use.

The Help tab is available to find help for R packages and functions. Don’t forget you can type ? before a function name in the console to get info in the Help section

Installing R packages

Once you have started RStudio, for the projects used in this session you may be asked to install additional R packages. Those packages can be downloaded and installed from the R Console or via point-and-click: Tools -> Install Packages -> Enter “package name” then click to install.

Packages contain functions, and all functions belong to packages. For example, read.csv belongs to the utils package.

R comes with about 30 packages (“base R”). There are over 10,000 user-contributed packages; you can discover these packages online.

Only install a package once. It will likely install several other packages it depends on.

Example: Installing Knitr

The knitr package can be installed via:

install.packages("knitr", dependencies = TRUE)

Note, when installing this (and other packages), the process will show comments of what it is downloading in red.

RStudio-install-knitr

RStudio-install-knitr

View and Change your Working Directory

The working directory is where R pulls files to work with. This is where your datasets, scripts, etc. live. It can be any folder location. (It doesn’t have to be the same folder where you installed R.)

R always has a working directory set. Get your working directory with this command in the console: getwd()

You can change the working directory a few ways.

You can set the working directory via point-and-click: Session (at the top) -> Set Working Directory -> Choose Directory

You can set the working directory via point-and-click from the File tab. Navigate to where you would like the working directory to be then select More -> Set as Working Directory

You can also set the working directory in the console.

This is an example of how I set my working directory using the console: setwd(“file/path/cs4821-cs5831”)

Verify that you have the right directory with getwd(). Note that you can see the working directory listed at the top of the Console.

RMarkdown

Look at the RStudio Documentation on how to create R Markdown files and convert them to HTML. For instance, this file can be converted to HTML using the Knit HTML button on the file install-R.Rmd.

The file can also be converted on the console with

library('rmarkdown')
render("install-R.Rmd")

The RMarkdown, html, and formatting files can be downloaded in the zip

HTML Style

CSS can be used to generate “better” formatted documents. The standard format can be replaced by adjusting the yaml header for this document to include a CSS file, specifically the file min.css.

---
title: "Introduction to R"
author: "cs4821-cs5831-s21"
date: "January 8, 2021"
output: 
  html_document:
  theme: null
  css: min.css
  highlight: null
---

Other formatting

Additional formating, templating and themes can be used. Details and resources can be found at R Markdown.

For example, use theme to specify a Boostrap theme to be used, e.g., cerulean, cosmo, spacelab, united, etc.. When using your own css style, the theme should be set to “null”, and the CSS style file specified in the css parameter.

The option of highlight can be used to select the syntax highlighting style, e.g., pygments, kate, monochrome, etc.

---
title: "test"
output:
  html_document:
    theme: flatly
    highlight: monochrome
---

Render with command line

This file was rendered from the command line using the following commands:

Rscript -e "rmarkdown::render('introduction.Rmd')"

The files needed to create this page are:

_output.yaml
introduction.Rmd
min.css
images\*

The _output.yml file consists of:

html_document:
  self_contained: true
  theme: null
  css: min.css
  highlight: null
  mathjax: null
  toc_depth: 3