15 Create an R package

Creating an R package can be done in a few easy steps.

usethis::create_package("~/Documents/GitHub/MyExamplePackage")
usethis::use_mit_license("J Kyle Armstrong")
usethis::use_readme_md()
usethis::use_pkgdown()
usethis::use_testthat()
usethis::use_package("ggplot2", "Imports", min_version = TRUE)
  • Imports is used for packages that are needed by your package but that don’t need to be loaded with library(). Packages referred to in @import or @importFrom statements in your Roxygen2 comments, or whose functions are accessed via the :: operator, should be here.

  • Suggests is for packages that aren’t really necessary, but that you’re using in your examples, vignettes, or tests. Any package listed in Imports will need to be installed with your package, while packages listed in Suggests do not need to be installed with your package.

tidyverse_packages <- c('tibble','readr','dplyr','tidyr','stringr','purrr','ggplot2','forcats')

tidyverse_friends <- c('broom','lubridate','readxl','knitr','shiny','furrr','flexdashboard','yardstick')

other_packages <- c('devtools','rsq','arsenal','skimr',
                    'RSQLite','dbplyr','plotly','DT','GGally','corrr',
                    'AMR','caret','shiny','DataExplorer','randomForest','usethis','testthat','pkgdown')

purrr::walk(c(tidyverse_packages, tidyverse_friends, other_packages),
function(package){
usethis::use_package(package, "Imports", min_version = TRUE)
}
)
devtools::load_all()
devtools::document()
devtools::install()
detach(package:MyExamplePackage)

15.1 How to Organize and Run Your Tests

We should have a folder named R with all the R code, and one folder named tests/testthat, where all the test scripts will live. For each file with R scripts there should be another one with tests, with the same name, but prefixed by test-, as shown:

library(testthat)
library(MyExamplePackage)

devtools::test()

15.2 Build package site

pkgdown::build_site()