Permalink
Browse files

Put a base solution in ex01

  • Loading branch information...
jennybc committed Apr 2, 2018
1 parent a8550f7 commit a0f1c7ed2368c8129a95fb4c81cbcf0d84c274dd
@@ -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.

@@ -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")
)
@@ -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.
@@ -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(
@@ -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)<!-- -->

0 comments on commit a0f1c7e

Please sign in to comment.