Data In The Wild
  1. Module 1
  2. 1.1: Introduction to R and RStudio
  • Home
  • Contact Us
  • For Instructors

  • Module 1
    • Overview
    • 1.1: Introduction to R and RStudio
    • 1.2: Intro to Coding in R
    • 1.3: Introduction to the tidyverse
    • Assignment 2
    • Assignment 3
  • Module 2
    • Overview
    • 2.1: Good Food Gone Bad
    • 2.2: Plotting with ggplot2
    • 2.3: Data Visualization
    • 2.4: Exploring geom Functions
    • 2.5: Module 2 Wrap-Up
    • Assignment 1
    • Assignment 2
    • Assignment 3
  • Module 3
    • Overview
    • 3.1: Leopard Seals
    • 3.2: T-Tests
    • 3.3: Comparing (Multiple) Means
    • Assignment 1
    • Assignment 2
  • Module 4
    • Overview
    • 4.1: Combining Datasets (Joins & Binds)
    • 4.2: K-Nearest Neighbor
    • 4.3: Roads and Regressions
    • 4.4: Multiple Regression
    • 4.5: Writing Functions
    • Assignment 1
    • Assignment 2
    • Assignment 3
    • Assignment 4
  • Module 5
    • Overview
    • 5.1: Population Growth
    • 5.2: Sustainable Fishing
    • 5.3: Comparing Populations
    • Assignment 1
    • Assignment 2
    • Assignment 3
  • Final Project

  • Resources

On this page

  • Introduction to R/RStudio
    • Learning Outcomes
    • Check-in (5 minutes)
    • RStudio Mini-tour
    • Using R as a Calculator
      • Using the Console
    • Quarto Documents and Code Chunks
      • Some Code Chunk Practice
      • Math: Small Group Challenge
  1. Module 1
  2. 1.1: Introduction to R and RStudio

1.1: Introduction to R and RStudio

Author

Ellen Bledsoe, Keaton Wilson, Lily McMullen

Introduction to R/RStudio

Learning Outcomes

  • Students will be able to describe what computer programming languages (code) are, with R as an example.
  • Students will be able to describe the role of RStudio in programming with R.
  • Students will be able to describe the utility of each panel in RStudio.
  • Students will be able to perform basic math functions in the R console.

Check-in (5 minutes)

Let’s all sign up for Posit Cloud! We use Posit Cloud as our coding environment so everyone has access to R and RStudio through a web browser.

RStudio Mini-tour

Let’s start with a brief live demo of RStudio. As you follow along, pay attention to:

  1. What do the four main panels allow you to do?
  2. The difference between the Console (runs code immediately) versus a script or Quarto file (lets you save and organize your work).

Instructor Note: Do a mini-tour of the coding environment. Cover:

  1. Each panel: script/editor (top-left), console (bottom-left), environment/history (top-right), files/plots/help (bottom-right).
  2. Running code in the Console vs. a chunk in a .qmd file, emphasizing that the Console doesn’t save your work, and code must be in chunks within the .qmd to run.

Using R as a Calculator

Using the Console

You can do basic math in the Console (the bottom left part of the screen). The Console only understands R code, but it can also be treated as a calculator! So we can type in some numbers and mathematical symbols.

Try multiplying 5 and 3 in the Console (hint: * means multiply). Hit Enter to run that line of code.

Quarto Documents and Code Chunks

Quarto (.qmd) is a file format that lets us incorporate text and code into one document seamlessly. In fact, it is the file format for this document!

  • For writing text, you can type as you normally would.
  • Code chunks are a bit different:
    • Near the top right of your screen you can toggle between viewing this document in “Source” or “Visual” mode.
    • In Source view, all code chunks are sandwiched between ```{r} and ```.
    • In Visual view, you type R code in the lines beneath {r}.
    • To add a comment (a note R won’t run as code), put a # in front of the line.

Code chunks look like this:

# This is a code chunk!

To run a chunk of code, click the green arrow on the top right corner of the chunk.

You can also run one or a few lines of code at a time by having your cursor on the line or highlighting multiple lines and hitting Ctrl + Enter (or Cmd + Enter on a Mac).

A quick shortcut for adding a code chunk is Ctrl + Alt + i (Cmd + Opt + i on a Mac). Alternatively, you can go to Code > Insert Chunk.

Instructor Note: Pause here and make sure everyone can toggle between Source and Visual mode and can run a chunk.

Some Code Chunk Practice

Let’s work with an example code chunk.

# The `#` symbol is used to write a comment. 
# Comments are notes to yourself or others reading your code. R does not run anything written after `#` on a line.

432 - 11     # subtraction
[1] 421
12 + 18      # addition
[1] 30
8 / 4        # division
[1] 2
3 * 5        # multiplication
[1] 15

Notice that if you run code in the Console rather than the code chunk in a .qmd file, you don’t need to add the {r} to tell it that you are typing R code. The Console only understands R code anyway, so we don’t need to tell it what it is!

Math: Small Group Challenge

Write a line of code to raise 2 to the 3rd power (often represented as \(2^3\)). This will likely require a little bit of Googling to figure out.

First, write and run the code in the console.

Next, write and run the code in the code chunk below. What did you have to do differently?

# Write your code here

Answer:

2^3
[1] 8

Instructor Note: The key difference students should notice is that in the Console you just type R code directly, while in a .qmd chunk you need the ```{r} wrapper.

2026, University of Arizona & Lewis & Clark College

 
  • Made with Quarto