Basics of R and RStudio

EE BIOL C177/C234

Chuliang Song

Today’s Menu 🎯

  1. Getting set up with R and RStudio
  2. First steps with basic R syntax
  3. Using AI as your coding assistant
  4. Managing and installing packages
  5. Meeting the tidyverse 🌍

Getting Set Up

Installing R and RStudio

Follow this installation guide to get started.

You need two things:

  1. R β€” the engine 🏎️
  2. RStudio β€” the dashboard πŸ–₯️

Why RStudio?

R alone = powerful but clunky

RStudio = a sleek interface that makes everything easier

  • Write & organize code
  • Visualize results instantly
  • Manage files and packages

For this course, RStudio is our primary IDE.

Other IDEs Worth Knowing

  • VS Code πŸ”— code.visualstudio.com
    • General-purpose, huge extension ecosystem
    • Best AI integration (Copilot, Cursor, etc.)
  • Positron πŸ”— positron.posit.co
    • Built by the same team behind RStudio
    • A bit in-between β€” not as R-native as RStudio, not as AI-integrated as VS Code
  • Antigravity πŸ”— antigravity.google
    • A new IDE from Google (a customized version of VS Code)
    • Native AI integration with Gemini and Claude
    • We will use it for agentic AI tasks towards the end of the course
  • All three are fine for this course, but RStudio is our default

Making Your Code Prettier

Two quick wins:

  1. Fira Code font β€” ligatures make <-, |>, != beautiful
  2. styler package β€” auto-format to tidyverse style (link)
# install.packages("styler")
# Then use the Addin in RStudio β†’ "Style active file"

First Steps in R

Hello, World! πŸ‘‹

The classic first program:

R as a calculator:

Storing Results in Variables

Why <- instead of =?

  • <- is for assignment (β€œput this value in that box”)
  • = is for function arguments (mean(x = c(1,2,3)))
  • Historical: early stats keyboards had a dedicated ← key!

Exercise

Create y = 5 Γ— 3, then z = y / 2:

Using AI πŸ€–

AI as Your R Assistant

Don’t memorize every function β€” let AI help!

But always verify AI suggestions:

Installing Packages πŸ“¦

Standard Approach

Install once, load every session:

Quirk Alert πŸ€”

install.packages("ggplot2") β€” quotes

library(ggplot2) β€” no quotes

Yes, it’s inconsistent. Welcome to R.

The :: Operator

pak::pak("ggplot2")

pak::pak() means: β€œuse the pak function from the pak package”

This lets you call a function without loading the whole package β€” avoiding name conflicts (a huge source of hidden bugs!).

The Tidyverse 🌍

Meet the Tidyverse

The tidyverse is an opinionated collection of R packages designed for data science.

What’s Inside?

Nine packages loaded at once:

Package Purpose
ggplot2 Data visualization
dplyr Data manipulation
tidyr Data tidying
readr Reading data
purrr Functional programming
tibble Modern data frames
stringr String manipulation
forcats Factor handling
lubridate Date/time handling

Handling Conflicts ⚠️

The filter and lag conflicts can cause silent bugs:

library(conflicted)
library(tidyverse)
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")

Or use the explicit package::function() syntax:

dplyr::filter(data, x > 5)

Want to avoid the annoying messages?

You can use the suppressPackageStartupMessages() function to hide the start-up output when loading packages:

suppressPackageStartupMessages(library(tidyverse))

Summary

What We Learned Today

  1. βœ… Install R + RStudio (or Positron)
  2. βœ… R basics: print(), arithmetic, <-
  3. βœ… AI tools + ? for help
  4. βœ… Install packages with pak
  5. βœ… Load the tidyverse

Next up: Data structures β€” vectors and tibbles! 🐧