Skip to content

Commit

Permalink
Merge pull request #55 from posit-conf-2023/short-links
Browse files Browse the repository at this point in the history
add to foot of functions 01 and iteration 01
  • Loading branch information
3mmaRand authored Sep 16, 2023
2 parents 99e4744 + d009147 commit 635465b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
10 changes: 4 additions & 6 deletions 01-functions-01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ author: "Emma Rand and Ian Lyttle <br>WiFi: Posit Conf 2023<br>Password: conf202
format:
revealjs:
theme: [simple, styles.scss]
footer: <https://pos.it/programming-r-conf-2023>
slide-number: true
chalkboard: true
code-link: true
Expand All @@ -25,7 +26,7 @@ This is a two-day, hands-on workshop for those who have embraced the tidyverse a

## Material

- <https://posit-conf-2023.github.io/programming-r/>
- pos.it/programming-r-conf-2023

## The team

Expand Down Expand Up @@ -147,7 +148,7 @@ You need a laptop with the following installed:
## Schedule {.smaller}

| Time | Activity |
|:-----------------|:-----------------------------------------------------|
|:------------------|:----------------------------------------------------|
| 09:00 - 10:30 | [Functions 1](01-functions-01.html) Introduction, vector and dataframe functions, embracing |
| 10:30 - 11:00 |*Coffee break* |
| 11:00 - 12:30 | [Functions 2](02-functions-02.html) Plot functions, style and side effects |
Expand Down Expand Up @@ -916,8 +917,7 @@ my_summary <- function(df, summary_var, group_var = NULL){
}
```


##
##

🎬 Try it out with more than one group

Expand All @@ -935,7 +935,6 @@ my_summary(penguins, bill_length_mm, c(species, island))
## Summary ☕

::: {style="font-size: 40%;"}

- Writing functions can make you more efficient and make your code more readable. This can be just for your benefit.

- Vector functions take one of more vectors as input; output can be a vector (useful in `mutate()` and `filter()`) or a single value (useful in `summarise()`)
Expand All @@ -947,7 +946,6 @@ my_summary(penguins, bill_length_mm, c(species, island))
- We use `{{ var }}` embracing to manage data masking

- We use `pick()` to select more than one variable

:::

## References
15 changes: 3 additions & 12 deletions 03-iteration-01.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ author: "Emma Rand and Ian Lyttle"
format:
revealjs:
theme: [simple, styles.scss]
footer: <https://pos.it/programming-r-conf-2023>
slide-number: true
chalkboard: true
code-link: true
Expand Down Expand Up @@ -91,7 +92,6 @@ We have:

- `across()` and `purrr()`


. . .

other languages, a for loop would be right after hello world
Expand Down Expand Up @@ -191,6 +191,7 @@ penguins |>
3 important arguments

## `across()` Arguments

- which columns you want to iterate over: `.cols = bill_length_mm:body_mass_g`

. . .
Expand Down Expand Up @@ -282,7 +283,6 @@ penguins |>

📢


```{r}
#| error: true
penguins |>
Expand Down Expand Up @@ -361,7 +361,6 @@ penguins |>
\(x) mean(x, na.rm = TRUE)))
```


## Anonymous functions

Note, You might also see:
Expand All @@ -371,13 +370,13 @@ penguins |>
summarise(across(ends_with("mm"),
~ mean(.x, na.rm = TRUE)))
```

. . .

- `\(x)` is base syntax new in in 4.1.0 **Recommended**

- `~ .x` is fine but only works in tidyverse functions


## `.funs`: calling more than one function

How can we use more than one function across the columns?
Expand Down Expand Up @@ -525,23 +524,18 @@ Time to bring together functions and iteration!

🎬 Write a function that summarises multiple specified columns of a data frame



``` r
my_summary <- function(df, cols) {

. . . .

}

```


``` r
my_summary(penguins, ends_with("mm"))
```


## A solution

```{r}
Expand All @@ -563,7 +557,6 @@ penguins |>
my_summary(ends_with("mm"))
```


## A improved solution

Include a default.
Expand All @@ -579,7 +572,6 @@ my_summary <- function(df, cols = where(is.numeric)) {
```


## Try it out

```{r}
Expand All @@ -588,7 +580,6 @@ penguins |>
my_summary()
```


## Summary

-
Expand Down

0 comments on commit 635465b

Please sign in to comment.