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.
.gitignore
file, and (c) select a license (The MIT license is a good fit for a demo R package).r-base-dev
): http://www.jason-french.com/blog/2013/03/11/installing-r-in-linux/.Install R packages (from within the RStudio 'Console'). The magrittr
package is for piping. The devtools
package makes package development much easier; its dependencies include testthat
(for testing), knitr
(for vignettes, and other reporting), roxygen2
(for easier documentation creation & maintenance), and covr
(for code coverage).
r
install.packages("devtools", dependencies=TRUE)
install.packages("magrittr")
* Install GitHub Desktop (for Windows & OSX): https://desktop.github.com/
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.
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
This is the most important file of the package structure.
DESCRIPTION
(without any extension) in the repo's root directory. Ideally, the Package name (in the first field) should match the repo's name. Alternatively, run devtools::create_description()
, and add the extra fields.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)
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.
.travis.yml
config file from https://github.com/OuhscBbmc/referral-exposure/blob/master/.travis.yml.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.
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.
appveyor.yml
config file from https://github.com/OuhscBbmc/referral-exposure/blob/master/appveyor.yml.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.
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.
Add r
, r-package
, and whatever is relevant to your content.
https://github.com/blog/2309-introducing-topics
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.
Copy and adapt from https://github.com/OuhscBbmc/referral-exposure/.
README.md
with badges and installation instructions.gitignore
.gitattributes
.Rbuildignore
For automated testing on Travis (#2) and AppVeyor (#3). Adapt from https://github.com/OuhscBbmc/referral-exposure/tree/master/tests.
[ ] Create ./tests/test-all.R
. Make sure there's an empty last line (not even a space or tab).
```r
library(testthat) library(ReferralExposure)
testthat::test_check("ReferralExposure")
```
[ ] Create .tests/testhat/rest-basic.R
. Make sure there's an empty last line (not even a space or tab).
```r library(testthat) context("Basic Functions")
test_that("smoke-test", { returned <- basic(3) expect_true(!is.null(returned)) })
test_that("vector-test", { expected <- 4:7 returned <- basic(3:6) expect_equal(returned, expected) })
```
[ ] Verify that this fails your local check (because we haven't yet created the function basic()
).
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 )
}
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
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
data/
inst/extdata/
Optional Topics
cran-comments.md
.devtools_build_win(version="R-devel")
is similar to AppVeyor and Travis, but maintained[r]
, [package]
, and/or [devtools]
: http://stackoverflow.com/search?q=devtools+r
R-package-devel
mailing list: https://stat.ethz.ch/mailman/listinfo/r-package-develrdevtools
Google Group: https://groups.google.com/forum/?utm_source=digest&utm_medium=email#!forum/rdevtools/topicsAdd the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.