```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# About this exercise

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.

## Purpose

**Skills you'll practice.** By the end of this exercise you will be able to:

1. Download an R Markdown file, save it in the right folder, and open it in RStudio.
2. Knit an R Markdown file to HTML and find the report it makes.
3. Explain why knitting runs your code in a fresh R session.
4. Check which version of R you have, which operating system and chip it runs on, and which version of RStudio you have.
5. Compare your version of R with the newest one on CRAN, and decide whether you need to update.
6. 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.

- [ ] The file knits to HTML with no errors, and the author and date at the top are yours.
- [ ] The file is saved in its own folder inside your `geog215` folder, not in Downloads or on the Desktop. (The path in Part 1 shows this.)
- [ ] Every `___` has been filled in, and every chunk runs.
- [ ] The pipe `|>` is used where the instructions ask for it.
- [ ] Every question (Q1 to Q10) has an answer in complete sentences.
- [ ] Each answer explains *why*, not just *what*. (See the examples below.)
- [ ] Your R is up to date (or newer than 4.1.0, with a reason you couldn't update), and the version printed at the bottom of your HTML matches your answer to Q10.
- [ ] The three reflection questions are answered.

| 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.

# Part 1. Save and Open the File (5 min)

A file you download usually lands in your **Downloads** folder. Don't work there: Downloads fills up with clutter, and later in the semester your data files need to sit next to your `.Rmd` file. Every exercise gets its own folder inside `geog215`.

- Make a folder called `r-setup-exercise` inside your `geog215` folder.
- Move this file (`r-setup-exercise.Rmd`) from Downloads into that folder. Check that the name still ends in `.Rmd`. Some browsers save it as `r-setup-exercise.Rmd.txt` or open it as text in a tab instead of downloading it. If that happens, right-click the download link and choose **Download Linked File** (Safari) or **Save Link As...** (Chrome, Edge, Firefox), and remove any `.txt` from the end of the name.
- In RStudio, choose **File > Open File...**, find the file in `geog215/r-setup-exercise`, and open it. (Double-clicking the file can open it in a text editor instead of RStudio.)
- Change `author:` at the top of the file to your name and `date:` to today's date.

When R runs the chunks of an `.Rmd` file, it starts in the folder where the file is saved. `getwd()` ("get working directory") prints that folder.

- Run the chunk below.

```{r where-am-i}
getwd()
```

**Q1.** What folder path did `getwd()` print? Does it end in `geog215/r-setup-exercise`? Why does it matter, later in the semester, that your `.Rmd` file and your data files are in the same folder?

*Your answer here.*

# Part 2. Knit, and See What a Fresh Session Means (8 min)

- Click **Knit** at the top of the Editor. If RStudio asks to install packages it needs (such as **rmarkdown**), click **Yes**.
- An HTML report should open in a preview window. Close it, then look in the **Files** pane (bottom right) at your `r-setup-exercise` folder.

**Q2.** What new file appeared in your folder after you knitted, and what is its name? How is it different from the `.Rmd` file?

*Your answer here.*

Now for an experiment. Chapter 2 says knitting runs your code in a **fresh** R session that knows nothing about what you've already run. `exists("my_secret")` returns `TRUE` if R has a variable called `my_secret`, and `FALSE` if it doesn't.

- In the **Console** (not in this file), type `my_secret <- 42` and press Enter. Look for `my_secret` in the Environment pane.
- Run the chunk below with its green arrow. Write down what it shows.

```{r secret-check}
exists("my_secret")
```

- Now knit the file again, and look at what this chunk shows in the HTML.

**Q3.** What did the chunk show when you ran it with the green arrow, and what did it show in the knitted HTML? Why are they different? What does this tell you about code you type only in the Console?

*Your answer here.*

# Part 3. Check Your Version of R (10 min)

R's version number has three parts. In R **4.6.1**, `4` is the **major** version, `6` is the **minor** version, and `1` is the **patch**. A new minor version (4.5 to 4.6) comes out each spring and adds new features; patches fix bugs.

You can see your version in three places:

- **The Console.** Choose **Session > Restart R**. The first line R prints in the Console starts with `R version`, followed by the version and its release date.
- **`R.version.string`.** R keeps the same line in a variable called `R.version.string`. Fill in the blank with it, and run the chunk.

```{r r-version-string}
___
```

- **`getRversion()`.** This function returns just the number, in a form R can compare with other version numbers. The pipe `|>` needs R **4.1.0** or newer. Fill in the blanks to store your version in `my_r_version` and check whether it is at least `"4.1.0"`.

```{r r-version-check}
my_r_version <- ___()
my_r_version
my_r_version >= "___"
```

The right download of R depends on your operating system and, on a Mac, on its chip. `Sys.info()` reports facts about your computer, and `R.version$arch` reports the chip R was built for: `aarch64` (or `arm64`) means an Apple silicon Mac (M1, M2, M3, and later) or another ARM chip, and `x86_64` means an Intel or AMD chip.

- Run the chunk below.
- Then fill in the blanks so the last line uses the pipe to turn your version into text with `as.character()`, and then count its characters with `nchar()`.

```{r my-computer}
Sys.info()[["sysname"]]   # Darwin is macOS
R.version$arch

my_r_version ___ as.character() |> ___()
```

**Q4.** Which version of R do you have? Which operating system does it run on, and which chip was it built for? Write the major, minor, and patch numbers separately.

*Your answer here.*

**Q5.** Did `my_r_version >= "4.1.0"` return `TRUE` or `FALSE`? What would happen if you ran code with `|>` in it on R 4.0? (Hint: R 4.0 has never heard of `|>`.)

*Your answer here.*

# Part 4. Check Your Version of RStudio (4 min)

Remember from Chapter 2: **R is the language, RStudio is the workspace.** They are two separate programs, with their own version numbers, and you update them separately.

- On a Mac, choose **RStudio > About RStudio**. On Windows or Linux, choose **Help > About RStudio**. Write down the version (it looks like a year and month, such as `2026.09.0+456`).
- Choose **Help > Check for Updates**. If RStudio says an update is available, you can install it after you finish this exercise.

(You can't check the RStudio version in a chunk: knitting runs outside RStudio, so code that asks RStudio questions fails when you knit.)

**Q6.** Which version of RStudio do you have? Which program actually runs your code, R or RStudio? If you update RStudio, does that update R too? Why or why not?

*Your answer here.*

# Part 5. Compare with the Newest R on CRAN (5 min)

**CRAN** (the Comprehensive R Archive Network) is where R and its packages come from.

- Go to <https://cran.r-project.org>. Under **Download and Install R**, click the link for your operating system. The page lists the newest version of R (for example, `R-4.6.1`).
- Fill in the blank with the newest version, in quotation marks, and run the chunk. It returns `TRUE` if your R is at least as new as CRAN's.

```{r compare-cran}
newest_r_on_cran <- "___"
my_r_version >= newest_r_on_cran
```

**Q7.** What is the newest version of R on CRAN? Is your R up to date? If not, is it behind by a **minor** version (for example, 4.5 when CRAN has 4.6) or only by a **patch** (4.6.0 when CRAN has 4.6.1)?

*Your answer here.*

# Part 6. Update R, Only If You Need To (15 min)

**If Q7 says your R is up to date, skip to Q8** and read the instructions for your operating system so you know what to do next spring.

Otherwise, follow the instructions for **your** operating system only. Before you start:

- Save this file, and write down your answers to Q4 and Q7 so you can compare later.
- Quit RStudio before you run the R installer. (R can't replace itself while it's running.)

## On a Mac

1. Go to <https://cran.r-project.org>, click **Download R for macOS**, and choose the installer that matches your chip from Part 3:
    - `aarch64` or `arm64` (Apple silicon: M1, M2, M3, ...): the file ending in **`-arm64.pkg`**.
    - `x86_64` (Intel): the file ending in **`-x86_64.pkg`**.
2. Open the `.pkg` file from Downloads and click through the installer with the default settings. It replaces your old version of R.
3. Open RStudio. It finds the new R by itself.

## On a Windows PC

1. Go to <https://cran.r-project.org>, click **Download R for Windows**, then **base**, then **Download R-x.y.z for Windows** (the newest version).
2. Run the `.exe` file from Downloads with the default settings. The installer puts the new R *next to* the old one; it doesn't remove the old one.
3. Open RStudio, choose **Tools > Global Options... > General**, and look at **R version**. If it doesn't show the new version, click **Change...**, choose the new version (or **Use your machine's default version of R**), click **OK**, and restart RStudio.
4. Once the new version works, you can remove the old one in **Settings > Apps > Installed apps**.

## On Linux

On Linux, R comes from your package manager, and the version in your distribution's own repository is often months behind CRAN. These commands are for **Ubuntu**; for other distributions, follow the instructions on CRAN's **Download R for Linux** page. Run them in RStudio's **Terminal** tab (next to the Console), *not* in the Console and not in this file. Each `sudo` line asks for your computer's password.

If you haven't added CRAN's repository before, add its signing key and the repository first:

```
sudo apt-get update
sudo apt-get install --no-install-recommends software-properties-common dirmngr
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
```

Then install or upgrade R:

```
sudo apt-get update
sudo apt-get install --only-upgrade r-base
```

(On Fedora, use `sudo dnf upgrade R`.) Then choose **Session > Restart R** in RStudio, or quit and reopen RStudio. RStudio on Linux uses whichever R the Terminal command `which R` finds.

## On a Chromebook or a lab computer

If you use RStudio in a web browser (such as Posit Cloud) or on a university lab computer, you can't update R yourself. Write which one you use in Q8, and check that Q5 returned `TRUE`.

**Q8.** Which operating system's instructions did you follow? What did you do: install a new R, or skip the update? If you skipped it, why?

*Your answer here.*

# Part 7. Check That the Update Worked (5 min)

When the **minor** version changes (4.5 to 4.6), R starts with a new, empty folder for packages. Your old packages are still on your computer, but the new R doesn't look for them there. `.libPaths()` shows the folder (or folders) where your R looks for packages.

- Run **Run > Restart R and Run All Chunks**. Check that the chunks in Part 3 now show your new version.
- Run the chunk below.

```{r package-folders}
.libPaths()
```

- If you updated to a new minor version, reinstall the packages you need from the **Packages** tab (**Install**), not with `install.packages()` in this file. For now you only need **rmarkdown** (RStudio will offer to install it when you knit) and, for Chapter 3, **spData**.

**Q9.** Look at the folder paths `.libPaths()` printed. Which part of a path shows the version of R the folder belongs to? Why would updating from R 4.5 to R 4.6 make your installed packages seem to disappear?

*Your answer here.*

`sessionInfo()` prints your version of R, your operating system, and the packages loaded, all in one place. It's a good line to put at the end of any report, so a reader (or a TA helping you) knows exactly what the code ran on.

```{r session-info}
sessionInfo()
```

**Q10.** What version of R did you have before this exercise (Q4), and what do you have now? Where in the knitted HTML can someone see which version of R made it?

*Your answer here.*

# Part 8. Reflection Questions (5 min)

1. How long did the exercise take you to complete?

*Your answer here.*

2. Where did you get stuck? Where did you go for help?

*Your answer here.*

3. Did you find any errors in the exercise?

*Your answer here.*

- Choose **Run > Restart R and Run All Chunks** one last time, then knit your file into HTML.
- Turn in your `.Rmd` file and your `.html` file on Canvas.
