[WIP] Module on "Workflow and piping" - #8
Conversation
willgearty
left a comment
There was a problem hiding this comment.
Some preliminary comments and thoughts. Let me know if you have any questions.
| # Same name gets overwritten | ||
| res <- head(mtcars, 10) | ||
| res <- subset(res, cyl >= 6) | ||
| res <- sort_by(res, ~ am) |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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)
|
|
||
| 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. |
There was a problem hiding this comment.
I wouldn't mention the pipe here, since we haven't defined it yet.
| 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 |
There was a problem hiding this comment.
And it is now recommended as part of the tidyverse style guide: https://style.tidyverse.org/pipes.html
| subset(cyl == 4) |> | ||
| (function(d) lm(mpg ~ disp, data = d))() | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Do we mention ->? I know some people that really like it because it works better with the sentence-like structure of piping.
|
|
||
| ## 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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)There was a problem hiding this comment.
I could even envision a nice slide setup where the code comes in one-by-one underneath the comments
| ```{r} | ||
| #| error: true | ||
| 1:3 |> mean | ||
| ``` |
There was a problem hiding this comment.
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 %>%?
There was a problem hiding this comment.
Maybe a collapsed div would be good?
| 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: |
There was a problem hiding this comment.
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.
| let |> | ||
| grepl("a|e", x = _) | ||
| ``` | ||
|
|
There was a problem hiding this comment.
This might be a good place to show off the equivalent of dplyr::pull():
cars |> _$speed |> mean()
We can refer to this: https://r4ds.hadley.nz/data-transform.html#sec-the-pipe