devtools::create(".")
in interactive session. 🚧
✅devtools::check()
and addressed errors. 🚧
✅Let’s build a cool ggplot2 extension function. And then let’s put it in a package. And test! Maybe it will be tedious, but more fun in good company?
‘Testing your code can be painful and tedious, but it greatly increases the quality of your code.’ - testthat introduction (probably Hadley Wickham)
In this workshop, we’ll build and strengthen package building and test writing muscles.
https://angeladuckworth.com/grit-scale/
Meeting objectives:
compute_group
easy geom extension by creating
geom_post().Prerequisite:
Having written a ‘compute group’ geom extension. See: https://evamaerey.github.io/mytidytuesday/2022-01-03-easy-geom-recipes/easy_geom_recipes.html Seasoned R/ggplot2 users mostly spent ~ 15 minutes on each recipe.
prize_wheel <- data.frame(probs = c(.7, .2, .1), payout = c(0, 1, 5))
library(ggplot2)
ggplot(prize_wheel) +
aes(x = payout, y = probs) +
geom_point() +
aes(xend = payout, yend = 0) +
geom_segment()
# would show just line
ggplot(prize_wheel) +
aes(x = payout, y = probs) +
geom_post()
# line and dot
ggplot(prize_wheel) +
aes(x = payout, y = probs) +
geom_point() +
geom_lollipop()
reference: https://evamaerey.github.io/mytidytuesday/2022-01-03-easy-geom-recipes/easy_geom_recipes.html
geom_lollipop <- function(...){
list(
geom_post(...),
geom_point(...)
)
}
devtools::create(".")
in interactive session. 🚧 ✅devtools::create(".")
Use a roxygen skeleton for auto documentation and making sure proposed functions are exported. Generally, early on, I don’t do much (anything) in terms of filling in the skeleton for documentation, because things may change.
Package dependencies managed, i.e. depend::function()
in proposed
functions and declared in the DESCRIPTION
usethis::use_package("ggplot2")
Use new {readme2pkg} function to do this from readme…
library(tidyverse)
readme2pkg::chunk_to_r("geom_post")
devtools::check()
and addressed errors. 🚧 ✅devtools::check(pkg = ".")
devtools::build()
The goal of the {ggtedious} package is to make it easy to draw posts (and to learn about package building and testing)
Install package with:
remotes::install_github("EvaMaeRey/ggtedious")
Once functions are exported you can remove go to two colons, and when things are are really finalized, then go without colons (and rearrange your readme…)
library(ggtedious)
usethis::use_mit_license()
usethis::use_lifecycle_badge("experimental")
Try to get feedback from experts on API, implementation, default decisions. Is there already work that solves this problem?
That would look like this…
library(testthat)
test_that("calc times 2 works", {
expect_equal(times_two(4), 8)
expect_equal(times_two(5), 10)
})
readme2pkg::chunk_to_tests_testthat("test_calc_times_two_works")
usethis::use_pkgdown()
pkgdown::build_site()
readLines("DESCRIPTION")