# This is a code chunk!1.1: Introduction to R and RStudio
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:
- What do the four main panels allow you to do?
- 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:
- Each panel: script/editor (top-left), console (bottom-left), environment/history (top-right), files/plots/help (bottom-right).
- Running code in the Console vs. a chunk in a
.qmdfile, emphasizing that the Console doesn’t save your work, and code must be in chunks within the.qmdto 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:
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 hereAnswer:
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.