Skip to content

Commit

Permalink
update post dry-run (#48)
Browse files Browse the repository at this point in the history
* fix typo

* first batch of changes

* small tweaks

* add pause
  • Loading branch information
ijlyttle authored Sep 9, 2023
1 parent d14efb8 commit d69c1ec
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
79 changes: 64 additions & 15 deletions 02-functions-02.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In this session, we will discuss:

:::{.incremental}
- embracing `{{}}` for `<data-masking>` functions
- "standard" style and design of functions
- tidyverse style and design of functions
- the joys of side-effects
:::

Expand All @@ -29,6 +29,7 @@ In this session, we will discuss:
For coding, we will use `r-programming-exercises`:

- `R/functions-02-01-embrace.R`, etc.
- with each new file, restart R.

## Plot functions: Motivation

Expand All @@ -54,17 +55,21 @@ diamonds |>

`aes()` is a data-masking function; you can embrace 🤗

- pass "bare-name" variables for data-frames
- look for `<data-masking>` in help

```{r}
#| output: false
histogram <- function(df, var, binwidth = NULL) {
df |>
ggplot(aes(x = {{ var }})) +
geom_histogram(binwidth = binwidth)
}
diamonds |> histogram(carat, 0.1)
histogram(diamonds, carat, 0.1)
```

## Your turn
## Our turn

Complete this function yourself:

Expand All @@ -84,15 +89,15 @@ histogram <- function(df, var, binwidth = NULL) {

- fill the bars with `"steelblue"`?

## Your turn (solution)
## Our turn (solution)

Adding a theme: `histogram()` returns a `ggplot` object, so you can add a theme in the "usual" way:

```{r}
histogram(starwars, height) + theme_minimal()
```

## Your turn (solution)
## Our turn (solution)

*As is*, there is no easy way to specify `"steelblue"`.

Expand All @@ -119,7 +124,7 @@ Passes unspecified arguments from your function to another (tell your users wher

[Tidyverse Design Guide](https://design.tidyverse.org/dots-after-required.html) has more details.

## Your turn (continued)
## Our turn (continued)

Incorporate dot-dot-dot into `histogram()`:

Expand Down Expand Up @@ -199,6 +204,7 @@ histogram(starwars, height) # "extra credit"
## Your turn (solution)

```{r}
#| output: false
histogram <- function(df, var, ..., binwidth = NULL) {
df |>
ggplot(aes(x = {{ var }})) +
Expand All @@ -225,7 +231,7 @@ sorted_bars <- function(df, var) {
geom_bar()
}
diamonds |> sorted_bars(clarity)
sorted_bars(diamonds, clarity)
```

## Mixing in other tidyverse functions
Expand Down Expand Up @@ -268,6 +274,12 @@ sorted_bars <- function(df, var) {

## Design and style

Restart R, open `functions-02-02-style.R`

. . .

<hr>

Use descriptive name, usually starts with a verb, unless it returns a well-known noun.

. . .
Expand Down Expand Up @@ -330,7 +342,14 @@ Why *optional* after *dots*?

## Namespacing functions

Are we using `filter()` from `{dplyr}` or `{stats}`?
When we write `filter()`, do we mean...

:::{.incremental}
- `dplyr::filter()`?
- `stats::filter()`?
:::

. . .

Three ways to sort this out:

Expand Down Expand Up @@ -371,7 +390,7 @@ mtcars |> filter(cyl == 6)

- run it as-is
- add `library("conflicted")`, run again
- add a `conflict_prefer()` directive
- add a `conflicts_prefer()` directive

## `package::function()`

Expand Down Expand Up @@ -446,10 +465,12 @@ Also, look at tidyverse code at GitHub (my favorite is [{usethis}](https://githu

## Side effects

There are two kinds of functions:
Restart R, open `functions-02-03-side-effects.R`

. . .

<hr>

**Pure function**:

- Returns a value that depends only on its inputs, e.g. `sum()`
Expand Down Expand Up @@ -526,7 +547,7 @@ Side effects can include:
- modifying options: `options()`
- setting random seed: `set.seed()`
- setting working directory: `setwd()`
- creating and wrting to a temporary file
- creating and writing to a temporary file
:::

. . .
Expand All @@ -539,13 +560,25 @@ Side effects can include:

```{r}
#| collapse: true
Sys.getlocale("LC_COLLATE")
(temp <- Sys.getlocale("LC_COLLATE"))
sort(c("apple", "Banana", "candle"))
```

. . .

<hr>

```{r}
#| collapse: true
Sys.setlocale("LC_COLLATE", "C")
sort(c("apple", "Banana", "candle"))
Sys.setlocale("LC_COLLATE", temp)
```

## Our turn: setting within call

To temporarily set locale:

```{r}
Expand Down Expand Up @@ -576,6 +609,22 @@ Sys.getlocale("LC_COLLATE")

*Within curly brackets* applies to function blocks, it also applies to {testthat} blocks.

## But what about dplyr?

:::{.incremental}
- `?dplyr::arrange()`
- `arrange()` uses the `"C"` locale by default
:::

. . .

<hr>

```{r}
tibble(text = c("apple", "Banana", "candle")) |>
arrange(text)
```

## Your turn

```{r}
Expand All @@ -588,7 +637,7 @@ test_that("mtcars has expected columns", {

This passes, but R is doing partial matching on the `$`.

Modify the `test_that()` block to warn on partial matching.
Modify `test_that()` block to warn on partial matching.

You can get the current setting using:

Expand Down Expand Up @@ -626,7 +675,7 @@ You can use tidy evaluation in {ggplot2} to specify aesthetics, add labels, and

<hr>

Using "standard" style and design makes things easier for you, your users, and *future you*.
Using tidyverse style and design can make things easier for you, your users, and *future you*.

. . .

Expand Down
2 changes: 1 addition & 1 deletion index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This is a one-day, hands-on workshop for those who have embraced the tidyverse a
### This workshop is for you if you...

- have experience equivalent to an introductory data science course using tidyverse
- feel comfortable with the [Whole game](https://r4ds.hadley.nz/whole-game.html) chapter of [R for Data Science (2nd Edition)](https://r4ds.hadley.nz/) by by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund.
- feel comfortable with the [Whole game](https://r4ds.hadley.nz/whole-game.html) chapter of [R for Data Science (2nd Edition)](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund.

### Please note

Expand Down

0 comments on commit d69c1ec

Please sign in to comment.