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

Correct display of embracing operator #1675

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions functions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ But you'll often also repeat the verbs themselves, particularly within a large p
When you notice yourself copying and pasting multiple verbs multiple times, you might think about writing a data frame function.
Data frame functions work like dplyr verbs: they take a data frame as the first argument, some extra arguments that say what to do with it, and return a data frame or a vector.

To let you write a function that uses dplyr verbs, we'll first introduce you to the challenge of indirection and how you can overcome it with embracing, `{{ }}`.
To let you write a function that uses dplyr verbs, we'll first introduce you to the challenge of indirection and how you can overcome it with embracing, `{{{ }}}`.
With this theory under your belt, we'll then show you a bunch of examples to illustrate what you might do with it.

### Indirection and tidy evaluation
Expand Down Expand Up @@ -398,11 +398,11 @@ The downside of tidy evaluation comes when we want to wrap up repeated tidyverse
Here we need some way to tell `group_by()` and `summarize()` not to treat `group_var` and `mean_var` as the name of the variables, but instead look inside them for the variable we actually want to use.

Tidy evaluation includes a solution to this problem called **embracing** 🤗.
Embracing a variable means to wrap it in braces so (e.g.) `var` becomes `{{ var }}`.
Embracing a variable means to wrap it in braces so (e.g.) `var` becomes `{{{ var }}}`.
Embracing a variable tells dplyr to use the value stored inside the argument, not the argument as the literal variable name.
One way to remember what's happening is to think of `{{ }}` as looking down a tunnel --- `{{ var }}` will make a dplyr function look inside of `var` rather than looking for a variable called `var`.
One way to remember what's happening is to think of `{{{ }}}` as looking down a tunnel --- `{{{ var }}}` will make a dplyr function look inside of `var` rather than looking for a variable called `var`.

So to make `grouped_mean()` work, we need to surround `group_var` and `mean_var` with `{{ }}`:
So to make `grouped_mean()` work, we need to surround `group_var` and `mean_var` with `{{{ }}}`:

```{r}
grouped_mean <- function(df, group_var, mean_var) {
Expand Down Expand Up @@ -782,7 +782,7 @@ rlang is a low-level package that's used by just about every other package in th

To solve the labeling problem we can use `rlang::englue()`.
This works similarly to `str_glue()`, so any value wrapped in `{ }` will be inserted into the string.
But it also understands `{{ }}`, which automatically inserts the appropriate variable name:
But it also understands `{{{ }}}`, which automatically inserts the appropriate variable name:

```{r}
#| fig-alt: |
Expand Down Expand Up @@ -861,7 +861,7 @@ density <- function(color, facets, binwidth = 0.1) {
}
```

As you can see we recommend putting extra spaces inside of `{{ }}`.
As you can see we recommend putting extra spaces inside of `{{{ }}}`.
This makes it very obvious that something unusual is happening.

### Exercises
Expand Down