documentation-for-developers/initializing-package.md

Initializing Package

Recommended steps for creating an R package, almost regardless of its content. The list below is roughly a copy-and-paste list into new Issues in your GitHub repository. This curriculum was developed by Will Beasley and Andrey Koval to serve the workshops for OU SCUG and Stanford's CIBSR.

Prerequisites:

Issues

Copy & paste each item into a new issue. Each should be assigned to the "package skeleton" milestone.

As this document grows older, some things may get out of date; R evolves, CRAN policies catch more mistakes, and coding styles change. Look at live packages for reference too. ReferralExposure might continue to be a simple project of ours, while REDCapR has more complicated dependencies, and hopefully should be maintained for many years. Of course, look at examples from Hadley and tidyverse, though their C code requires much more complexity than your first package will.

Establish Rproj file

It's important to set good build/check defaults. Copy from https://github.com/OuhscBbmc/referral-exposure/blob/master/referral-exposure.Rproj. In the repo's root directory, create a plain text file; the name should match the repo, and have the extension 'Rproj' (case-sensitive). Alternatively, run devtools::use_rstudio(), and add the extra fields.

Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: No

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

Establish DESCRIPTION file

This is the most important file of the package structure.

Package: ReferralExposure
Title: Calculating exposure over multiple referrals
Description: Reusable functions for calculating exposure during repeated spans.
Version: 0.0.1.9000
Date: 2017-02-21
Authors@R: c(person("Will", "Beasley", role = c("aut", "cre"), email =
    "wibeasley@hotmail.com"), person("David", "Bard", role = "ctb"))
URL: https://github.com/OuhscBbmc/referral-exposure, http://ouhsc.edu/bbmc/
BugReports: https://github.com/OuhscBbmc/referral-exposure/issues
Depends:
    R(>= 3.0.0),
    stats
Imports:
    methods,
    magrittr
Suggests:
    devtools,
    knitr,
    rmarkdown,
    testthat (>= 0.9)
License: MIT + file LICENSE
LazyData: TRUE
RoxygenNote: 6.0.1
Roxygen: list(markdown = TRUE)

Establish Travis-CI

Travis-CI tests your package on different Linux environments. It starts with a new environment/machine every build, so it flushes out deployment problems that may not manifest on your development machine.

Further details: The first test will take Travis a long time (maybe 20 min?) because it's caching a lot of the environment setup. Once cached, a minimal package will take just a few minutes each push. Only the last commit of a push is tested. Travis-CI can do a lot of things beyond this simple scenario. There's a lot to read. * https://travis-ci.org/getting_started * https://docs.travis-ci.com/user/languages/r/ Alternatively, use devtools::use_travis() and complete the extra fields.

Establish AppVeyor Optional

AppVeyor tests your package on different Windows environments. It starts with a new environment/machine every build, so it flushes out deployment problems that may not manifest on your development machine.

Further details: The first test will take AppVeyor a long time (but not as long as Travis) because it's caching a lot of the environment setup. Once cached, a minimal package will take just a few minutes each push. Only the last commit of a push is tested. AppVeyor can do a lot of things beyond this simple scenario. There's a lot to read. * https://www.appveyor.com/docs/ * https://github.com/krlmlr/r-appveyor Alternatively, use devtools::use_appveyor() and complete the extra fields.

Branching Demo

This isn't a real issue for the package. It's a placeholder for the SCUG meeting. While the Travis & AppVeyor environments are building & caching, we'll spend ~15 minutes covering branching/forking, merging, and rebasing.

See https://github.com/wibeasley/class-branching.

Establish GitHub "topics"

Add r, r-package, and whatever is relevant to your content.

https://github.com/blog/2309-introducing-topics

Establish dev branch

All the real work should be committed to this development branch. Pull the finished features in to the master branch with Pull Requests.

Any changes that happen in the master, away from the dev, use rebase to update dev.

Establish repo-wide config files

Copy and adapt from https://github.com/OuhscBbmc/referral-exposure/.

Establish first unit tests

For automated testing on Travis (#2) and AppVeyor (#3). Adapt from https://github.com/OuhscBbmc/referral-exposure/tree/master/tests.

Establish first function

Create .R/basic.R. Make sure there's an empty last line (not even a space or tab).

#' Short title
#'
#' Longer sentence description
#'
#' @param a integer.  Required
#' @export
#' @importFrom magrittr %>%
#' @md
#'
basic <- function( a ) {
  b <- a + 1

  return( b )
}

Package-wide documentation

Assume your username is 'uuu'. Assume your package name is 'ppp'. Create ./R/ppp-package.R. Make sure there's an empty last line (not even a space or tab).

#' @docType package
#' @name ppp-package
#' @aliases ppp
#'
#' @title Short-book-title
#'
#' @description
#' The description should be a longer complete sentence.
#'
#' TODO: consider adding links to your project's funders.
#'
#' @note
#' The most recent development version is available through [GitHub](https://github.com/uuu/ppp) by
#' running.
#'
#' `devtools::install_github('uuu/ppp')`
#' (make sure [devtools](https://cran.r-project.org/package=devtools) is already installed).
#' If you're having trouble with the package, please install the development version.  If this doesn't solve
#' your problem, please create a [new issue](https://github.com/uuu/ppp/issues), or email uuu.
#'
#' @examples
#' \dontrun{
#' # Install/update ReferralExposure with the development version from GitHub
#' #install.packages('devtools') #Uncomment if `devtools` isn't installed already.
#' devtools::install_github('uuu/ppp')
#' }
NULL

Next Steps

After the package structure has been initialized, attendees will add functions to their own packages. So please come with ideas for recurring challenges/tasks faced in your personal research. Depending on attendees' interests, we can discuss:

Important Topics

  1. Identifying which parts of your research challenges/tasks can be cast as a problem for a common tool/function.
  2. Strategies for coding these common tasks.
  3. Approaches to testing the reusable functions with an automated test suite that runs at every checkpoint of development.
  4. Making the reusable functions robust to corner cases and input you haven't anticipated.
  5. Adding internal and external data sets.
    • rda files in data/
    • csv files in inst/extdata/

Optional Topics

  1. Providing support to users with reasonable boundaries.
  2. Code coverage with covr.  (Example with REDCapR package.
  3. "Releases" and versioning.
  4. Creating helpful documentation that minimizes maintenance.
  5. Supplementing documentation with vignettes.
  6. Online documentation with RDocumentation.org and pkgdown
  7. Using repositories to supplement publications.
  8. Assigning a DOI to repository with Zenodo. Benefits of archiving by a 3rd party.
  9. Tradeoffs of deploying your package to CRAN.
    • Explanatory notes to CRAN maintainers with cran-comments.md.
    • devtools_build_win(version="R-devel") is similar to AppVeyor and Travis, but maintained

Further Reading & References



wibeasley/r-package-skeleton documentation built on May 8, 2019, 8:52 a.m.