+ - 0:00:00
Notes for current slide
Notes for next slide

You're already ready:

Zen and the Art of R Package development

Malcolm Barrett

@malco_barrett

Slides: bit.ly/rsg-zenrpkgs

1
In R, the fundamental unit of
shareable code
is the package.

— Hadley Wickham

2
3
If you do not see the Way,
you do not see it
even as you walk on it

— Sandokai

4
If you do not see the R Package,
you do not see it
even as you develop it
5

You already
structure your project

6
├── data/
├── reports/
├── scripts/
└── analysis.Rproj
7
├── data/
├── reports/
├── R/
└── analysis.Rproj
8
├── data/
├── reports/
├── R/
└── analysis.Rproj
9
├── data/
├── vignettes/
├── R/
└── analysis.Rproj
10
├── data/
├── vignettes/
├── R/
├── man/
├── tests/
├── DESCRIPTION
├── NAMESPACE
└── analysis.Rproj
11

library(usethis)
create_package("../zenartrpkgs")

12

library(usethis)
create_package("../zenartrpkgs")

Creating 'zenartrpkgs'
Creating 'R/'
Writing 'DESCRIPTION'
Writing 'NAMESPACE'
Writing 'zenartrpkgs.Rproj'
Adding '.Rproj.user' to '.gitignore'
Adding '^zenartrpkgs\\.Rproj' to '.Rbuildignore'
Setting active project to 'zenartrpkgs'

13
├── data/
├── vignettes/
├── R/
├── man/
├── tests/
├── DESCRIPTION
├── NAMESPACE
└── analysis.Rproj
14
Package: zenartofrpkgs
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "Malcolm",
family = "Barrett",
role = c("aut", "cre"),
email = "malcolmbarrett@gmail.com",
comment = c(ORCID = "0000-0003-0299-5825"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
15

create_project("../zenartrpkgs")

Creating 'zenartrpkgs'
Creating 'R/'
Writing 'zenartrpkgs.Rproj'
Adding '.Rproj.user' to '.gitignore'
Setting active project to 'zenartrpkgs'

16

create_project("../zenartrpkgs")

✓ Creating 'zenartrpkgs'
✓ Creating 'R/'
✓ Writing 'zenartrpkgs.Rproj'
✓ Adding '.Rproj.user' to '.gitignore'
✓ Setting active project to 'zenartrpkgs'

use_description()

Writing 'DESCRIPTION'

17

create_project("../zenartrpkgs")

✓ Creating 'zenartrpkgs'
✓ Creating 'R/'
✓ Writing 'zenartrpkgs.Rproj'
✓ Adding '.Rproj.user' to '.gitignore'
✓ Setting active project to 'zenartrpkgs'

use_description()

✓ Writing 'DESCRIPTION'

usethis:::is_package()
[1] TRUE

18

You already
write R code

19

iris %>%
  group_by(Species) %>%
  summarize(mean_sepal_length = mean(Sepal.Length))

# A tibble: 3 x 2
  Species    mean_sepal_length
  <fct>                  <dbl>
1 setosa                  5.01
2 versicolor              5.94
3 virginica               6.59

20

use_r("summarize_data")

Modify 'summarize_data.R'

21

use_r("summarize_data")

● Modify 'summarize_data.R'

# in 'R/summarize_data.R'
summarize_iris <- function() {
  iris %>%
    group_by(Species) %>%
    summarize(
      mean_sepal_length = mean(Sepal.Length)
    )
}

22

library(devtools)
load_all() # Cmd/Ctrl + Shift + L

Loading zenartrpkgs

23

library(devtools)
load_all() # Cmd/Ctrl + Shift + L

Loading zenartrpkgs

summarize_iris()

# A tibble: 3 x 2
  Species    mean_sepal_length
  <fct>                  <dbl>
1 setosa                  5.01
2 versicolor              5.94
3 virginica               6.59

24

You already
declare your dependencies

25

library(dplyr)
library(ggplot2)

plot_iris <- function() {
  iris %>%
    group_by(Species) %>%
    summarize(
      mean_sepal_length = mean(Sepal.Length)
    ) %>%
    ggplot(aes(mean_sepal_length, Species)) +
    geom_point()
}

26

library(dplyr)
library(ggplot2)

plot_iris <- function() {
  iris %>%
    group_by(Species) %>%
    summarize(
      mean_sepal_length = mean(Sepal.Length)
    ) %>%
    ggplot(aes(mean_sepal_length, Species)) +
    geom_point()
}

27

# use_pipe()
use_package("dplyr")

Adding 'dplyr' to Imports field in DESCRIPTION
Refer to functions with `dplyr::fun()`

28

# use_pipe()
use_package("dplyr")

✓ Adding 'dplyr' to Imports field in DESCRIPTION
● Refer to functions with `dplyr::fun()`


use_package("ggplot2")

Adding 'ggplot2' to Imports field in DESCRIPTION
Refer to functions with `ggplot2::fun()`

29

plot_iris <- function() {
  iris %>%
    group_by(Species) %>%
    summarize(
      mean_sepal_length = mean(Sepal.Length)
    ) %>%
    ggplot(aes(mean_sepal_length, Species)) +
    geom_point()
}

30

plot_iris <- function() {
  iris %>%
    dplyr::group_by(Species) %>%
    dplyr::summarize(
      mean_sepal_length = mean(Sepal.Length)
    ) %>%
    ggplot2::ggplot(
      ggplot2::aes(mean_sepal_length, Species)
    ) +
    ggplot2::geom_point()
}

31
Package: zenartofrpkgs
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "Malcolm",
family = "Barrett",
role = c("aut", "cre"),
email = "malcolmbarrett@gmail.com",
comment = c(ORCID = "0000-0003-0299-5825"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
32
Package: zenartofrpkgs
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person(given = "Malcolm",
family = "Barrett",
role = c("aut", "cre"),
email = "malcolmbarrett@gmail.com",
comment = c(ORCID = "0000-0003-0299-5825"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports:
ggplot2
dplyr
33

# install all dependencies in DESCRIPTION
install_deps()

34

You already
test your code

35

clean_data(iris)

Error: Something went wrong

36

clean_data(iris)

Error: Something went wrong

emo::ji("thinking")

🤔

37

clean_data(iris[, -5])

Cleaned `iris`

emo::ji("thinking")

🤔

38

use_test("clean_data")

Adding 'testthat' to Imports field in DESCRIPTION
Creating 'tests/testthat/'
Writing 'tests/testthat.R'
Call `use_test()` to initialize a basic test file and open it for editing.
Increasing 'testthat' version to '>= 2.1.0' in DESCRIPTION
Writing 'tests/testthat/test-clean_data.R'

39

test() # Cmd/Ctrl + Shift + T

Loading zenartrpkgs
Testing zenartrpkgs


── Results ──────────────────
OK:       1
Failed:   0
Warnings: 0
Skipped:  0

40
41

Use a description file

42

Use a description file

Write code as functions

43

Use a description file

Write code as functions

Write down your tests; automate them

44

Coming home
to R packages

45

an invitation

47

an invitation

create a package: a personal R package, something for your work, or turn a project into a package

48
The bad news is you’re falling
through the air, nothing to hang on to, no parachute.
The good news is there’s no ground.

— Chögyam Trungpa Rinpoche

49
> message
[1] "thanks for coming!"
> name
[1] "Malcolm Barrett"
> twitter
[1] "@malco_barrett"
> github
[1] "@malcolmbarrett"
> website
[1] "https://malco.io/"
> r_courses
[1] "https://malco.io/training"
> slides_made_with
[1] "xaringan"
> also
[1] "xanringanthemer" "xaringanExtra"
> art_by
[1] "Kaz Tanahashi" "Jikihara Gyokusei"
50
In
R,
the
fundamental
unit
of
shareable
code
is
the
package.

— Hadley Wickham

2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow