Skip to content

Latest commit

 

History

History
185 lines (123 loc) · 3.26 KB

Day2.md

File metadata and controls

185 lines (123 loc) · 3.26 KB

Package Development Day 2

Joyce Robbins

Welcome

Joyce Robbins jtr13@columbia.edu Twitter: @jtrnyc

Thank you to Emily Dodwell and Erin Grand of RLadiesNYC for organizing.

Thank you to Mine Çetinkaya-Rundel and Emma Rand of RForwards for developing package development workshop modules.

Review

library(devtools)
create_package("newpackage")
load_all()
document()
install()

How do I add a documentation template (@param, @return, etc.?)

add <- function(x, y) {
  x + y
}

Package states

Source: https://r-pkgs.org/package-structure-state.html#installed-package

Key points:

  • devtools::install(): source –> installed

  • install.packages(): binary –> installed

  • Developers submit bundles to CRAN, CRAN builds and distributes binaries

Functions

  • Scripts vs. functions

  • Parameter defaults

  • Return

Example:

# missing by column
library(mi)
data(CHAIN)
sapply(CHAIN, function(x) sum(is.na(x)))

More info: Functions chapter in R for Data Science

https://r4ds.had.co.nz/functions.html

Run-time testing

x <- 4
if (x != 3) stop ("x must equal 3.")
if (x != 3) warning ("x isn't equal to 3.")
if (x != 3) message ("Just letting you know that x equals ", x)

Dependencies

usethis::use_package("dplyr")

Use “Imports” not “Suggests, Depends”

Checking package

What are we checking and why?

“Using R CMD check, the R package, checker, one can test whether source R packages work correctly.”

https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Checking-packages

“R CMD check automatically checks your code for common problems. It’s essential if you’re planning on submitting to CRAN, but it’s useful even if you’re not because it automatically detects many common problems that you’d otherwise discover the hard way.”

https://r-pkgs.org/r-cmd-check.html

Your options

How often should you check and what should you fix (errors, warnings, notes, and/or nothing)?

Julia Silge, “How I Learned to Stop Worrying and Love R CMD Check”

https://juliasilge.com/blog/how-i-stopped/

devtools::check()

License

usethis::use_mit_license("Joyce Robbins")

https://choosealicense.com/

devtools::check()

Package names

  • Unique name you can easily Google

  • Avoid mixing upper and lower case (GGally)

  • Use abbreviations

  • Add an r to make unique, e.g stringr

  • Use wordplay, e.g. r2d3, forcats, officer

  • Avoid trademarked names

  • Use the available package to check name not taken

available::available_on_cran("covidtime")
available::available_on_github("covidtime")

Vignettes

Read vignettes!

usethis::use_vignette("first_package", title = "Creating your first package")

Package web site

usethis::use_readme_rmd()
usethis::use_pkgdown()
devtools::build_site()

Extras

usethis::edit_r_profile()
options(
  usethis.full_name = "Joyce Robbins",
  usethis.description = list(
    `Authors@R` = 'person("Joyce", "Robbins",
                   email = "joycerobbins1@gmail.com",
                   role = c("aut", "cre"))'
    License = "MIT + file LICENSE",
    Version = "0.0.0.9000"
  )
)

References

R Packages

Writing R Extensions