tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.78k stars 2.12k forks source link

Add the Output of Example Tibbles in Programming with Dplyr #4488

Closed dylanjm closed 4 years ago

dylanjm commented 5 years ago

I've read through Programming with Dplyr several times and have really started to understand the content and use it in my regular coding practices.

I've reread it several times because each time I come back to it, I have fresh eyes and learn something new. One hangup I've noticed with each read, is that my mind seems to churn harder when parsing what the example df looks like before applying a change to it.

In my mind, I'm trying to transpose (literally flip) the code into what it would look like and it gets more complicated when adding "shortcuts" like x = 1:3 vs. x = c(1, 2, 3).

I propose simply adding the output of df just as is done with the output of the changes to the df.

I realize there is a balance between content vs. code, but perhaps might lessen the mental load on readers.

For example, I would change

df <- tibble(x = 1:3, y = 3:1)
filter(df, x == 1)
#> # A tibble: 1 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3

to this

df <- tibble(x = 1:3, y = 3:1)
df
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

filter(df, x == 1)
#> # A tibble: 1 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3

Or it could be (Without explicitly evaluating df):

df <- tibble(x = 1:3, y = 3:1)
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

filter(df, x == 1)
#> # A tibble: 1 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3

This 2nd option is a little more clean but might be confusing to readers and not work well with the .Rmd. Would you all accept a PR for this change?

romainfrancois commented 5 years ago

You can also use ( to force printing, e.g.

library(dplyr, warn.conflicts = FALSE)

(df <- tibble(x = 1:3, y = 3:1))
#> # A tibble: 3 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3
#> 2     2     2
#> 3     3     1

filter(df, x == 1)
#> # A tibble: 1 x 2
#>       x     y
#>   <int> <int>
#> 1     1     3

However, I believe (@lionel- knows more) that the Programming vignette is being replaced by the tidyeval book: https://tidyeval.tidyverse.org

hadley commented 4 years ago

We're going to revamp this whole doc in #4407, and this is definitely something we'll bear in mind.