[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)
There was a problem hiding this comment.
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 ~.
There was a problem hiding this comment.
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 link to reference docs like in pkgdown...
|
|
||
| ## 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
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Sounds like this comes down to personal preference. It's probably fine as-is for now.
| # 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.
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 link to reference docs like in pkgdown...
|
|
||
| ## 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.
Sounds like this comes down to personal preference. It's probably fine as-is for now.
Co-authored-by: William Gearty <willgearty@gmail.com>
We can refer to this: https://r4ds.hadley.nz/data-transform.html#sec-the-pipe