Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update slides for 2024. #50

Merged
merged 9 commits into from
Oct 1, 2024
32 changes: 32 additions & 0 deletions lessons/9-intermediate-r-2/lesson_script2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(dplyr)
library(tidyr)
library(ggplot2)
library(palmerpenguins)

penguins <- penguins

# Fit a linear model on Biscoe penguins and extract predictions

biscoe <- penguins |>
filter(island == "Biscoe") |>
filter(!is.na(sex),
!is.na(body_mass_g))

biscoe_lm <- lm(body_mass_g ~ sex * species,
data = biscoe)

summary(biscoe_lm)

biscoe_lm_predictions <-
biscoe |>
select(sex, species) |>
distinct()

biscoe_lm_predictions <- biscoe_lm_predictions |>
mutate(predicted = predict(biscoe_lm, newdata = biscoe_lm_predictions))

# How could we break this into a function?

# Use a for loop to apply this function to each island and combine the results.

# Use purrr::map to apply this function to each island and combine the results.
121 changes: 72 additions & 49 deletions lessons/9-intermediate-r-2/slides.qmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Intermediate R II: Functions cont'd, iteration, and getting help"
subtitle: "Session 9"
date: "2023-10-01"
title: "Intermediate R II: Iteration and more functions"
subtitle: "Session 10"
date: "2024-10-01"
date-format: long
format:
uaz-revealjs:
Expand All @@ -12,73 +12,96 @@ format:
footer: "[Reproduciblity & Data Science in R](../../index.html)"
---

# Before we jump in...
# Learning objectives

## Show and tell (Late October)
- Write a more advanced function
- Set up an analysis with function and analysis scripts
- Use *iteration* to streamline workflows involving functions

- An opportunity to apply workshop skills to your own work.
- You could apply different parts to different projects, or more integratively to one single project.
- Optional, but encouraged!
# When should I write my own function?

## Some inspirations
- Standardized procedures done many times
- Discrete chunks of a workflow
- Code used by multiple scripts or at multiple points in a workflow

- Turn the project folder for one of your current research project into a git/GitHub repo and/or R project
- Modify an R script to use functions, conditionals, or best-practices for code formatting
- Improve directory organization for project or your entire computer!
# When should I leave code in a script?

# Today's objectives
- Single-use, highly-customized code (e.g. a plot)
- Code that is changing a lot
- Code that *tells the story* of your analysis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!


- Modify our data cleaning function to apply to different islands
- Use iteration to apply this function to each island
- Organize our workflow into "functions" and "running" scripts
- Learn how to get help with more advanced topics
# Questions to ask yourself

# Part I: Generalizing a function
- Do I find myself typing the same code over and over?
- Am I copy-pasting and changing a single variable/character?
- Does this code distract from the readability of a script?
- Is there an existing function I could pull from an R package?

- Open up your Rproject from Tuesday
- Coding objectives:
- Make sure the function we wrote works
- Apply this function to different islands by swapping URLS
- Add "ifelse" logic to avoid having to re-type the URL every time
# Coding time

# Part II: Iteration
- Review sample analysis to decide what could be a function
- Organize this analysis into analysis and function scripts

## Iteration by hand
# Iteration

``` r
my_first_site <- read.csv("data_first.csv")
my_second_site <- read.csv("data_second.csv")
my_third_site <- read.csv("data_third.csv")
# Iteration
diazrenata marked this conversation as resolved.
Show resolved Hide resolved

- Iteration allows you to repeat the same process many times on different objects.
- `for` loops:
- Run sequentially
- Easy to write and read
- Slower, can be inefficient
- `purrr::map`:
- *Can* run in parallel
- Fairly readable
- More efficient

# Anatomy of `for` loops

```
for(<thing> in <some things>) {
<do something with or to the thing>
}
```

```
for(i in 1:10) {
print(i^2)
}
```

## Why use code to iterate?
# Storing the output of `for` loops

- Less code: fewer bugs, easier to understand
- Easier to apply to new situations
- Faster to write, maybe faster to run
```
diazrenata marked this conversation as resolved.
Show resolved Hide resolved

## Iteration using for loops
squares <- vector(length = 10)

- Coding objectives:
- Write a for loop to apply our cleaning function to each island
- Store the results in a list
- Convert this list back to a dataframe
for(i in 1:10){
squares[i] <- i^2
}
```

## Iteration using purrr::map
# Anatomy of `purrr::map`

- Coding objectives:
- Use `purrr::map` as an alternative to a for loop
- Demonstrate `purrr` applied to more complex workflows
```
diazrenata marked this conversation as resolved.
Show resolved Hide resolved
purrr::map(<some things>, <a function>)
```

# Part III: Getting help
```
diazrenata marked this conversation as resolved.
Show resolved Hide resolved
purrr::map(1:10, \(x) x^2)
```

- See the [Getting Help](../11-getting-help/slides.html) section!
# Storing the output of `purrr::map`

```
diazrenata marked this conversation as resolved.
Show resolved Hide resolved
purr_squares <- purrr::map(1:10, \(x) x^2)
```

# Wrap-up
# Coding time

Commit this week's work and create a new PR.
- Practice using a for loop and a map statement to apply our function to each island in turn.

## Homework
# Resources

- Identify a section of your code that could be made more efficient using a for loop or iterating using a function
- Start thinking about show-and-tell opportunities!
- [R4DS on iteration](https://r4ds.hadley.nz/iteration.html)
- [`purrr`](https://purrr.tidyverse.org/) and [`furrr`](https://furrr.futureverse.org/) documentation