Chapter exercise: R, RStudio, and R Markdown
Is Your Computer Ready for R?
This exercise goes with Chapter 2, R, RStudio, and R Markdown. It should take you about one hour or less. Read this whole section before you start.
Save it in a new folder called r-setup-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.
Purpose
Skills you’ll practice. By the end of this exercise you will be able to:
- Download an R Markdown file, save it in the right folder, and open it in RStudio.
- Knit an R Markdown file to HTML and find the report it makes.
- Explain why knitting runs your code in a fresh R session.
- Check which version of R you have, which operating system and chip it runs on, and which version of RStudio you have.
- Compare your version of R with the newest one on CRAN, and decide whether you need to update.
- Update R on a Mac, a Windows PC, or a Linux computer, and make RStudio use the new version.
Knowledge you’ll gain. You’ll find out exactly which version of R your computer runs, whether it is new enough for this course, and how to keep it up to date for the rest of the semester.
Why this matters. Every lab and assignment this semester starts the same way: download an .Rmd file, open it in RStudio, run the chunks, and knit. If any of those steps goes wrong, you can’t start the real work. An old version of R causes trouble that is hard to spot: the pipe |> doesn’t exist before R 4.1, and new versions of packages often refuse to install on old versions of R. Checking your setup now, in a low-stakes exercise, means you won’t find out the night an assignment is due. Knowing your version also makes it much easier for the instructor or a TA to help you when something breaks.
Task
Work through Parts 1 to 8 in order, top to bottom. In each part:
- Read the short explanation, then do each step marked with a bullet (•).
- Wherever you see
___, replace it with the missing code before you run the chunk. A chunk with___in it won’t run. - Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here. Delete Your answer here. when you write your answer.
- Run each chunk with the green arrow or Ctrl+Enter (Cmd+Return on a Mac), and look at the output before you move on.
In Part 6 you only follow the instructions for your operating system (Mac, Windows, or Linux). If your R is already up to date, you skip the install, so you may finish early.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Save and open the file | 5 min |
| 2 | Knit, and see what a fresh session means | 8 min |
| 3 | Check your version of R | 10 min |
| 4 | Check your version of RStudio | 4 min |
| 5 | Compare with the newest R on CRAN | 5 min |
| 6 | Update R (only if you need to) | 15 min |
| 7 | Check that the update worked | 5 min |
| 8 | Reflect, knit, and turn in | 5 min |
Turn in your .Rmd file and your knitted .html file on Canvas.
Criteria for success
Use this checklist before you turn your work in. Each item matches a row of the rubric below.
| Criterion | Full credit | Partial credit | No credit |
|---|---|---|---|
| Knits (2 pts) | Knits to HTML with no errors | Knits, but some chunks have errors shown | Doesn’t knit, or no HTML turned in |
| Folder (2 pts) | File saved in its own folder inside geog215 |
Inside geog215, but not in its own folder |
In Downloads, on the Desktop, or somewhere else |
| Code (3 pts) | Every blank filled in correctly, pipe used where asked; every chunk runs | Most blanks correct | Many blanks missing or wrong |
| Questions (8 pts) | Q1 to Q10 answered correctly, with reasons | Some answers missing, wrong, or with no reason | Most answers missing |
| Up to date (4 pts) | R matches the newest version on CRAN, or the student explains why it couldn’t be updated and R is 4.1.0 or newer | R is older than CRAN’s but 4.1.0 or newer, with no reason given | R is older than 4.1.0 |
| Reflection (1 pt) | All three answered | Missing |
What a good answer looks like. Here is a question, a weak answer, and a strong one.
Question: Why do your packages seem to disappear after you update R from 4.5 to 4.6?
Weak answer: “Because I updated.”
Strong answer: “Each minor version of R (4.5, 4.6) keeps its packages in its own library folder. The new version starts with an empty folder, so the packages I installed for 4.5 are still on my computer but R 4.6 doesn’t look there. I need to install them again for 4.6.”
The strong answer says what happened, why it happened, and what to do about it. It doesn’t need to be long.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
.Rmd) that mixes formatted text with
chunks of R code. Knitting it runs the code and builds a finished
report.
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
One run of R, from start to finish. A new session starts with an empty
Environment and no packages loaded; knitting always starts a fresh one.
The Comprehensive R Archive Network: the official website for
downloading R and most R packages.
A block of R code inside an R Markdown file, starting with
```{r} and ending with three backticks.
The operator |>, which passes the result on its left as
the first argument of the function on its right. Read it as “and then.”
A bundle of code, written by other users, that adds new functions to R.
Install it once with install.packages(), then load it in
each session with library().
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.