Meet R

You have a codebook. You have (or will soon have) a dataset. Now you need a tool to analyze it. That tool is R, a free, open-source programming language built for data analysis and statistics. This chapter gets you set up and comfortable before the real work begins. You will run R inside VS Code, the same editor you use for the rest of the course.

Why R?

You might be wondering: Why can’t I just use Excel? You can do some of this in Excel. But R gives you three things Excel doesn’t:

  1. Reproducibility. Every step of your analysis is written as code. You can re-run it, share it, and verify it. In Excel, clicking through menus leaves no trail.
  2. Publication-ready output. R + Quarto renders your analysis directly into a professional PDF or website. No copy-pasting charts into Word.
  3. Statistical power. R handles chi-square tests, effect sizes, and visualizations that would require expensive add-ons in Excel.

You’re not becoming a programmer. You’re learning to use a specific tool for a specific purpose: turning your coded data into findings you can communicate.

R inside VS Code

With the R extension installed (see Install R), VS Code becomes your analysis environment. Four areas do the work:

┌─────────────────────┬─────────────────────┐
│                     │                     │
│   Editor            │   Workspace         │
│   (where you write  │   (your data and    │
│    code in .qmd     │    variables, via   │
│    and .R files)    │    the R extension) │
│                     │                     │
├─────────────────────┼─────────────────────┤
│                     │                     │
│   R terminal        │   Plots panel       │
│   (where code runs  │   (your charts      │
│    and output       │    render here)     │
│    appears)         │                     │
│                     │                     │
└─────────────────────┴─────────────────────┘
  • Editor (center): Where you write and edit your .qmd and .R files. This is where most of your work happens. Press Ctrl/Cmd+Enter to send a line to R.
  • R terminal (bottom panel): Where R actually runs your code. You can type commands here directly, but usually you’ll send code from the editor.
  • Workspace (R extension sidebar): Shows what data and variables R currently has loaded. When you import a dataset, it appears here.
  • Plots panel: Opens automatically when you draw a chart.

There is no separate “project file.” In VS Code you open your workspace folder, and that folder is your working directory.

Setting up your environment

Let’s verify everything works. Complete these steps in order, and don’t skip ahead.

Step 1: Verify R is installed

Open VS Code, open a terminal (Terminal, New Terminal), type R to start a session, and run:

R.version.string

You should see something like "R version 4.4.2 (2024-10-31)". The exact version doesn’t matter as long as it starts with 4.

Step 2: Install and load required packages

Packages are collections of functions that extend what R can do. You need two:

install.packages("tidyverse")

install.packages("remotes")
remotes::install_github("AURA-Lab-SIUE/v2v-r")

tidyverse is the standard collection of R packages for data work: importing, reshaping, summarizing, and charting. v2v is the course package. It ships the Twitch dataset this workbook uses, the course figure theme, and a set of helpers written for these assignments.

Note that the two names in that second command are different on purpose. You install from the repository v2v-r, and you load the package v2v. The package is not on CRAN, so install.packages("v2v") will not find it.

If the install gives you trouble, or you hit a prompt you are not sure how to answer, the Installing the v2v package guide covers every error and prompt this step produces.

This may take a few minutes. You’ll see red text scrolling, which is normal. When it’s done, load them:

library(tidyverse)
library(v2v)

If you see no errors, you’re good. If R reports there is no package called 'v2v', the install did not finish; run the two install lines again and read the output for the real error. If the tidyverse install is the one that fails, try it again with dependencies = TRUE:

install.packages("tidyverse", dependencies = TRUE)
WarningCommon Windows issue

If R says it can’t install a package because a file is “in use,” close any other R sessions and try again. Windows sometimes locks files that are open in another session.

Step 3: Open your workspace folder

Keeping your files in one folder makes file paths work correctly.

  1. Create a folder you can find easily, for example Documents/MC451_Analysis (no spaces).
  2. In VS Code: File, Open Folder, and select it.
  3. That folder is now your working directory for the whole R session. Inside it, make a data/ subfolder for your datasets.

Step 4: Test with the Twitch dataset

There is nothing to download. The course dataset ships inside the v2v package, so you load it with a function call instead of a file path:


library(tidyverse)
library(v2v)

chat <- twitch_chat()
streams <- twitch_streams()

glimpse(chat)

chat is one row per chat message. streams is one row per snapshot of one stream at one moment. glimpse() prints every column, its type, and its first few values:

Rows: 35,267
Columns: 5
$ id      <int> 89, 551, 1033, 2094, 2803, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", "sodapoppin", "xqcow", …
$ sender  <chr> "madzee", "prometheanow", "spectre155", "itsjer", "laserfru…
$ message <chr> "???????????????????????", "TriEasy Clap TriEasy Clap TriE…
$ date    <dbl> 1542578127023, 1542578135562, 1542578143610, 1542578157677…

If you see that table in the R terminal and chat appears in the Workspace panel, everything is working.

Notice what did not happen. There was no file path to get wrong, no download step, no format to guess at. A new R user’s first encounter with data is usually a pile of path errors, and twitch_chat() and twitch_streams() exist to remove that whole category of problem. Your own project data, later in the course, will arrive the ordinary way, through a file. This one arrives through a function.

Step 5: Check your environment with v2v::setup()

The course package carries its own diagnostic:

v2v::setup()

This verifies your environment: R, the packages the workbook depends on, and whether the versions agree. Fix anything it flags. Run it again any time you suspect an installation problem rather than a code problem.

Note the v2v:: in front of the function name. It tells R, and tells you, that setup() comes from the course package specifically. Functions you have loaded with library(v2v) work without the prefix, but writing it out makes clear which tools are the course’s and which are R’s own.

Quarto documents (.qmd)

All of your R assignments are written in Quarto documents, files ending in .qmd. A Quarto document mixes text and code in a single file. When you “render” it, Quarto runs your R code, captures the output (tables, charts, numbers), and produces a polished PDF or HTML document.

The structure of a .qmd file

A Quarto document has three parts:

1. YAML header (at the very top, between --- marks):

---
title: "My Analysis"
author: "Your Name"
format: pdf
---

2. Text (written in plain language, just like any document):

This analysis examines the relationship between channel type
and message length in the Twitch dataset.

3. Code chunks (R code between special markers):

```{r}
library(tidyverse)
library(v2v)
chat <- twitch_chat()
```

When you click Render in the Quarto preview (or press Ctrl/Cmd+Shift+K), Quarto:

  1. Runs all the R code chunks in order
  2. Captures their output (tables, charts, printed results)
  3. Combines the text and output into a finished document
  4. Saves it as PDF or HTML (whichever you specified)

This is exactly how your White Paper will work: one Quarto project that renders into both a PDF and a website.

Try it yourself

  1. In your open workspace, create a new file named my-first-doc.qmd.
  2. Add a YAML header with a title, then a code chunk:
library(tidyverse)
library(v2v)
chat <- twitch_chat()
nrow(chat)
  1. Press Ctrl/Cmd+Shift+K to render (or click Render in the Quarto preview).
  2. You should see a PDF (or HTML) with the number 35267, the number of chat messages in the dataset.

If this works, you’re ready for Chapter 4.

TipConnection to your project

Every assignment from here forward is a .qmd file. Your White Paper is a Quarto book, a collection of .qmd files that render together. The skills you’re building now (writing text plus code in the same document) are the exact skills you’ll use to publish your research.