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 iteration 2 #50

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions 04-iteration-02.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ In this session, we will discuss:
For coding, we will use `r-programming-exercises`:

- `R/iteration-02-01-reading-files.R`, etc.
- restart R

## Reading multiple files

Expand Down Expand Up @@ -94,8 +95,10 @@ Run this example code, discuss with your neighbor.

I see this as a two step problem:

:::{.incremental}
- make a named list of paths, name is year
- use list of paths to read data frames, combine
:::

. . .

Expand Down Expand Up @@ -129,35 +132,50 @@ data <-

If we have a failure, we may not want to stop everything.

We can use a function-operator to return a modified function.
. . .

```{r}
#| error: true
library("readxl")
read_excel("not/a/file.txt")
library("readr")
read_csv("not/a/file.csv")
```

. . .

<hr>
## Function operators

Function operators:

- take a function
- return a modified function

. . .

```{r}
#| error: true
library("purrr")
poss_rdxl <- possibly(read_excel, otherwise = NULL, quiet = TRUE)
poss_rdxl("not/a/file.txt")
poss_read_csv <-
possibly(read_csv, otherwise = NULL, quiet = TRUE)

poss_read_csv("not/a/file.csv")
```

. . .

<hr>
## Function operators

Function operators:

- take a function
- return a modified function

```{r}
#| message: true
poss_rdxl <- possibly(read_excel, otherwise = NULL, quiet = FALSE)
poss_rdxl("not/a/file.txt")
poss_read_csv <-
possibly(read_csv, otherwise = NULL, quiet = FALSE)

poss_read_csv("not/a/file.csv")
```


## Our turn: handle failure

In the `programming-r-exercises` repository:
Expand Down Expand Up @@ -192,10 +210,6 @@ Functional programming has three fundamental operations:

Implement `list_rbind()` using functional-programming techniques:

- *filters* in not-`NULL` values, `purrr::keep()`
- *maps* name of element to data-column, `purrr::imap()`
- *reduces* list to single data-frame, `purrr::reduce()`

``` r
list_rbind2 <- function(df, names_to) {
df |>
Expand All @@ -205,6 +219,12 @@ list_rbind2 <- function(df, names_to) {
}
```

:::{.incremental}
- *filters* in not-`NULL` values, `purrr::keep()`
- *maps* name of element to data-column, `purrr::imap()`
- *reduces* list to single data-frame, `purrr::reduce()`
:::

## Our turn: saving multiple outputs

**Goal**: write out a csv file *for each* value of `clarity` within ggplot2's `diamonds` dataset.
Expand All @@ -228,8 +248,7 @@ When we read "for each", we might think of using `map()`:
`iteration-02-02-writing-files.R`

``` r
# ?dplyr::group_nest
# ?stringr::str_glue
# ?dplyr::group_nest(), ?stringr::str_glue(), ?readr::write_csv

# from diamonds, create tibble with columns: clarity, data, filename
by_clarity_csv <-
Expand All @@ -238,8 +257,6 @@ by_clarity_csv <-
# create column for filename
identity()

# ?readr::write_csv

# using the data and filename, write out csv files
walk2(
by_clarity_csv$data,
Expand All @@ -256,15 +273,22 @@ walk2(

<hr>

Tactic: create a `plot` column, where each element is a ggplot.
Create a `plot` column, where each element is a ggplot.

This will be a list-column.

. . .

You can use `map()` within `mutate()`, with all the tidy-eval goodness!
You can use `map()`:

(To me, this feels like a superpower 💪)
- within `mutate()`, with all the tidy-eval goodness!
- with additional arguments (after the function), e.g.:

```r
mutate(
plot = map(data, histogram, binwidth = 0.1)
)
```

## Our turn: starter-code

Expand All @@ -281,7 +305,6 @@ by_clarity_plots <-
identity()

# ?ggplot2::ggsave

# using the data and filename, write out plots to png files
walk2(
by_clarity_plot$filename,
Expand All @@ -296,9 +319,11 @@ Three fundamental operations in functional programming

Given a list and a function:

:::{.incremental}
- `filter()`: make a new list, subset of old list
- `map()`: make a new list, operating on each element
- `reduce()`: make a new "thing"
:::

## dplyr using purrr?

Expand Down Expand Up @@ -339,8 +364,6 @@ I claim it's possible, I don't claim it's a good idea.
## `dpurrr_filter()`

```{r}
library("tidyverse")

dpurrr_filter <- function(df, predicate) {
df |>
as.list() |>
Expand Down