[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 like 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.
| * 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 |
There was a problem hiding this comment.
| ### Intermediate objects | |
| ### Intermediate objects |
| ### 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: |
There was a problem hiding this comment.
I would move the second half of this sentence to between the two code blocks
| - 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. |
There was a problem hiding this comment.
I wonder if it would make more sense to move each of these up to after each example.
| # 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 like 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.
We can refer to this: https://r4ds.hadley.nz/data-transform.html#sec-the-pipe