1. What Are R & RStudio?

2. Installation of R and RStudio

If you have used R or RStudio before, I recommend uninstalling the old versions and installing the latest version of R and RStudio since some libraries that we’ll install might not be compatible with older versions of R.

See the YouTube Videos below from OpenIntro Statistics for a video tutorial of

or you can install following the text instructions below.

2.1 Install the latest version of R

Go to https://cran.r-project.org/. Select Download R for Windows, MacOS, or Linux depending on your operation system to the downloading page for the installation file.

  • Windows: Click “Download R x.y.z for Windows”. Then run downloaded .exe file to install. Agree to all of the installation defaults (unless you already know how to customize R).

  • MacOS: Click on one of the “R-x.y.z.pkg” file depending on the version of your Mac OS to begin the installation. You can leave the default options as is just like for Windows. Please note that you might have to (re)install Xquartz if you use Mac OS X.

  • Linux: I assume Linux users know how to install software on it yourself

2.2 Install the latest RStudio

Download RStudio Desktop. Select an installer based on whether you use Windows or Mac and then install.

2.3 RStudio Layout

The RStudio interface consists of several windows (see the Figure below).

RStudio Layout

  • Bottom left: command window or Console. Here you can type simple commands after the “>” prompt and R will then execute your command. This is the most important window, because this is where R actually does stuff.

  • Top left: Source Editor, or Script window Collections of commands (scripts) can be edited and saved. When you don’t get this window, you can open it with [File] – [New] – [R script]. Just typing a command in the editor window is not enough, it has to get into the command window before R executes the command. If you want to run a line from the script window (or the whole script), you can click Run or press Ctrl+Enter (Windows) or Cmd+Return (Mac) to send it to the Console.

  • Top right: workspace / history window. In the workspace window you can see which data and values R has in its memory. You can view and edit the values by clicking on them. The history window shows what has been typed before.

  • Bottom right: files / plots / packages / help window. Here you can open files, view plots (also previous plots), install and load packages or use the help function.

You can change the size of the windows by dragging the grey bars between the windows.

3. A Brief Demo of R Markdown

R Markdown is a simple formatting syntax for producing PDF, MS Word, and HTML documents that incorporates R codes and R output in one single source document. It can save you tons of time copying/pasting R text output or exporting/importing R plots into your homework. We will do a short demo of R markdown in Lab 1 and introduce it in detail in Lab 2.

Please download the R markdown (RMD) file lab01work.Rmd, and open it in RStudio. It will be opened in the [Source Editor] as follows.

---
title: "STAT 234 Lab 1"
author: "Your Name Goes Here"
output: pdf_document
---

Hello! World. 

This is my first R Markdown document. 

```{r}
plot(sunspot.month)
```

In the file, the first few lines that begin and end with 3 dashes (---) are called the header. which specifies the title, author, output format, and the other meta setting of the R markdown file.

The remaining part is called the body of the R markdown.

The portion below is called an R code chunk.

```{r}
plot(sunspot.month)
```

In general, R codes in an R markdown document must be enclosed in R code chunks that begin with (three back-ticks (`) followed by an “r” enclosed in curly braces) and ends with another three back-ticks

```{r}
first line of R codes
second line of R codes
... more lines of R codes

```

Please note one can type back-tick (`) using the key on the top right corner of an US keyboard.

back-tick on Keyboard

The code chunk contains only one line of R codes plot(sunspot.month), where sunspot.month is a built-in dataset in R that stores the monthly sunspot counts data from 1749 to present. To execute the command, One can place the cursor on the line plot(sunspot.month), and press [Ctrl + Enter] (Windows) or [Cmd + Enter] (Mac). The output appears directly below the code.

Outside of the R code chunk(s), one can add text to an R Markdown document, like Hello! World in the lab01work.Rmd file. One can add some description for the data, the data analysis methods, and the conclusion of the analysis to the R markdown file.

Knitting R Markdown to PDF

Please click the [Knit] button in RStudio and choose “Knit to PDF”. A PDF document should be generated like this.

If “Knit to PDF” doesn’t work, please install tinytex package in R by executing the two lines below in the [Console].

install.packages("tinytex")
tinytex::install_tinytex()

It might take 10+ minutes to download and install tinytex depending the speed of your internet.

Once tinytex is installed, you can knit the RMD to PDF again and it should work.

If you still have trouble knitting RMD to PDF, you can choose “Knit to Word” and then convert the knitted Word document into PDF yourself.

This is the end of Lab 1. A detailed introduction to R markdown will be given in Lab 2.


Last Update: September 27, 2022