Skip to content

[WIP] Module on "Workflow and piping" - #8

Draft
etiennebacher wants to merge 7 commits into
mainfrom
module-workflow-piping
Draft

[WIP] Module on "Workflow and piping"#8
etiennebacher wants to merge 7 commits into
mainfrom
module-workflow-piping

Conversation

@etiennebacher

@etiennebacher etiennebacher commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

@github-actions

Copy link
Copy Markdown

@willgearty willgearty left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some preliminary comments and thoughts. Let me know if you have any questions.

Comment thread workflow_piping/index.qmd
# Same name gets overwritten
res <- head(mtcars, 10)
res <- subset(res, cyl >= 6)
res <- sort_by(res, ~ am)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sort_by is a relatively new function (I'm not sure I knew it existed). Maybe a more classic res[order(res$am), ] would be better (or could be used as an even more "classical" approach)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we do leave it, we may want to devote some time to explain the formula syntax (I don't think a lot of beginners are familiar with it)

Comment thread workflow_piping/index.qmd

Let's say we want to keep the first 10 rows in the `mtcars` data, then keep the observations where `cyl >= 6`, and finally sort the remaining data by the `am` column.

Without using the pipe, we have two main approaches.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wouldn't mention the pipe here, since we haven't defined it yet.

Comment thread workflow_piping/index.qmd
Its design and popularity inspired the implementation of `|>` in base R in 2021.

For simple cases, `|>` and `%>%` behave identically.
We recommend using `|>` simply because it is always available in R and doesn't rely on an external package. No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And it is now recommended as part of the tidyverse style guide: https://style.tidyverse.org/pipes.html

Comment thread workflow_piping/index.qmd
subset(cyl == 4) |>
(function(d) lm(mpg ~ disp, data = d))()
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we mention ->? I know some people that really like it because it works better with the sentence-like structure of piping.

Comment thread workflow_piping/index.qmd

## Example where we don't use the pipe

Let's say we want to keep the first 10 rows in the `mtcars` data, then keep the observations where `cyl >= 6`, and finally sort the remaining data by the `am` column.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would make this a bulleted list of actions to be taken. Then it will be easy to track how each bullet translates into each line of code below

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You could even have the three lines be comments in the code, for example:

# keep the first 10 rows in the `mtcars` data
res <- head(mtcars, 10)
# keep the observations where `cyl >= 6`
res <- subset(res, cyl >= 6)
# sort the remaining data by the `am` column
res <- sort_by(res, ~ am)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I could even envision a nice slide setup where the code comes in one-by-one underneath the comments

Comment thread workflow_piping/index.qmd
Comment on lines +104 to +107
```{r}
#| error: true
1:3 |> mean
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While technically true, I wonder if this could confuse people more than help them (since I don't imagine they would be thinking of doing this). Is the inclusion of this in case people are used to %>%?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe a collapsed div would be good?

Comment thread workflow_piping/index.qmd
So far, we have only used named functions provided in base R (though we could have used functions from other packages).
Sometimes, it is necessary to run custom code on the data without creating a dedicated new function for that.

To do so, we need to wrap the call to the anonymous function in parenthesis and evaluate it with `()` at the end of its definition:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A lot of people probably won't know the difference between an anonymous function and a nonanonymous function. I would start out with defining it and giving an example. Then move on to the piping example.

Comment thread workflow_piping/index.qmd
let |>
grepl("a|e", x = _)
```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might be a good place to show off the equivalent of dplyr::pull():

cars |> _$speed |> mean()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants