Skip to content

Commit

Permalink
Put a base solution in ex01
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Apr 2, 2018
1 parent a8550f7 commit a0f1c7e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
25 changes: 20 additions & 5 deletions ex01_leave-it-in-the-data-frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ options(tidyverse.quiet = TRUE)

#+ body
# ----
#' ## Two code styles

#' ## Don't create odd little excerpts and copies of your data.
#'
#' Code style that results from (I speculate) minimizing the number of key
#' presses.

Expand All @@ -25,20 +25,35 @@ sl <- iris[51:100,1]
pw <- iris[51:100,4]
plot(sl ~ pw)

#' This clutters the workspace with "loose parts", `sl` and `pw`. Very soon, you
#' are likely to forget what they are, which `Species` of `iris` they represent,
#' and what the relationship between them is.

# ----
#' ## Leave the data *in situ* and reveal intent in your code
#'
#' More verbose code conveys intent. Eliminating the Magic Numbers makes the
#' code less likely to be, or become, wrong.

## :) version 1
#'
#' Here's one way to do same in a tidyverse style:
library(tidyverse)

ggplot(
filter(iris, Species == "versicolor"),
aes(x = Petal.Width, y = Sepal.Length)
) + geom_point()

## :) version 2, using the pipe operator, %>%
#' Another tidyverse approach, this time using the pipe operator, `%>%`
iris %>%
filter(Species == "versicolor") %>%
ggplot(aes(x = Petal.Width, y = Sepal.Length)) + ## <--- NOTE the `+` sign!!
geom_point()

#' A base solution that still follows the principles of
#'
#' * leave the data in data frame
#' * convey intent
plot(
Sepal.Length ~ Petal.Width,
data = subset(iris, subset = Species == "versicolor")
)
35 changes: 29 additions & 6 deletions ex01_leave-it-in-the-data-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Leave your data in that big, beautiful data frame
Jenny Bryan
2018-04-02

## Two code styles
## Don’t create odd little excerpts and copies of your data.

Code style that results from (I speculate) minimizing the number of key
presses.
Expand All @@ -17,11 +17,18 @@ plot(sl ~ pw)

![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-2-1.png)<!-- -->

This clutters the workspace with “loose parts”, `sl` and `pw`. Very
soon, you are likely to forget what they are, which `Species` of `iris`
they represent, and what the relationship between them is.

## Leave the data *in situ* and reveal intent in your code

More verbose code conveys intent. Eliminating the Magic Numbers makes
the code less likely to be, or become, wrong.

Here’s one way to do same in a tidyverse style:

``` r
## :) version 1
library(tidyverse)

ggplot(
Expand All @@ -30,15 +37,31 @@ ggplot(
) + geom_point()
```

![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-3-1.png)<!-- -->
![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-4-1.png)<!-- -->

``` r
Another tidyverse approach, this time using the pipe operator, `%>%`

## :) version 2, using the pipe operator, %>%
``` r
iris %>%
filter(Species == "versicolor") %>%
ggplot(aes(x = Petal.Width, y = Sepal.Length)) + ## <--- NOTE the `+` sign!!
geom_point()
```

![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-3-2.png)<!-- -->
![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-5-1.png)<!-- -->

A base solution that still follows the principles of

- leave the data in data frame
- convey intent

<!-- end list -->

``` r
plot(
Sepal.Length ~ Petal.Width,
data = subset(iris, subset = Species == "versicolor")
)
```

![](ex01_leave-it-in-the-data-frame_files/figure-gfm/unnamed-chunk-6-1.png)<!-- -->
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a0f1c7e

Please sign in to comment.