Skip to content

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

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

[WIP] Module on "Workflow and piping"#8
etiennebacher wants to merge 8 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)

@etiennebacher etiennebacher Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It was introduced in R 4.4 (two years ago) and I had chosen it because it's easy to read and fits very nicely in the various syntaxes presented here. Using res[order(res$am), ] would be annoying in the case of nested calls and when we show the example with the pipe below.

I agree the ~ might be slightly unexpected but I still think the intent of the function is clear without devoting some space to explain ~.

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.

Oh, you might be surprised to hear how old some people's installed R versions are...But I do agree it reads well. In general for these modules, I think it's better to include more detail rather than less. In this case, the formula format seems to be the intended way for sort_by(), so it's probably fine.

Related but not for this PR: I wonder if we could have functions like to reference docs like in pkgdown...

Comment thread workflow_piping/index.qmd Outdated
Comment thread workflow_piping/index.qmd Outdated
Comment thread workflow_piping/index.qmd
Comment thread workflow_piping/index.qmd Outdated

## 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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I moved this to a bulleted list but I think the code is simple enough to avoid comments on each line (also because we repeat the same code several times with different syntaxes, so it's better if we can keep it "clean" without comments IMO).

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.

Sounds like this comes down to personal preference. It's probably fine as-is for now.

Comment thread workflow_piping/index.qmd Outdated
Comment thread workflow_piping/index.qmd Outdated
Comment thread workflow_piping/index.qmd
Comment thread workflow_piping/index.qmd
* sort the remaining data by the `am` column.

We could do this in two different ways: by assigning intermediate output or by nesting function calls.
### Intermediate objects

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.

Suggested change
### Intermediate objects
### Intermediate objects

Comment thread workflow_piping/index.qmd
### Intermediate objects

First, we can assign each function output to an object.
This object can either keep the same name and be overwritten at each step, or we can use a collection of temporary names:

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 move the second half of this sentence to between the two code blocks

Comment thread workflow_piping/index.qmd
Comment on lines +54 to +56
- in the first case, if we want to rename `res` in the future then we must be careful to rename all its occurrences throughout the code. It also means that if, say, the call to `sort_by()` is wrong, then we must run the entire block again so that `res` is properly reset. Depending on the data size and operations to run, this can be very time-consuming.

- in the second case, we pollute the global environment with potentially many temporary objects. Additionally, using a counter in a temporary name means that we need to update many names if we want to add an operation between the first and second step for instance.

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 wonder if it would make more sense to move each of these up to after each example.

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.

Oh, you might be surprised to hear how old some people's installed R versions are...But I do agree it reads well. In general for these modules, I think it's better to include more detail rather than less. In this case, the formula format seems to be the intended way for sort_by(), so it's probably fine.

Related but not for this PR: I wonder if we could have functions like to reference docs like in pkgdown...

Comment thread workflow_piping/index.qmd Outdated

## 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.

Sounds like this comes down to personal preference. It's probably fine as-is for now.

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