devtools(Wickham et al. 2022) approachYou should have
devtools and assertthat
By the end of this session you will able to:
usethis:use_r()
devtools::load_all()
devtools::check() to execute R CMD check
usethis::use_mit_license()
roxygen2 and devtools::document()
usethis::use_package()
Conventionally:
But you can also make a package just for your own use! Or just for a data analysis project!
If you are already trying to work reproducibly you are almost doing it anyway!
/stem-cell-proteomic
/data-raw
/data-processed
/figures
/R
README.md
report.Rmd
stem-cell-proteomic.RProj
β¦ making a package is just a short step beyond that
Future self: CC-BY-NC, by Julen Colomb, derived from Randall Munroe cartoon
CRAN:
GitHub:
Bioconductor
In a library! In
[1] "C:/Users/er13/AppData/Local/R/win-library/4.5"
[2] "C:/Program Files/R/R-4.5.2/library"
A library is a folder
Base packages live in C:/Program Files/R/R-4.5.2/library
There are five states a package can be in:
source
bundled
binary
installed
in-memory
source
bundled
binary
installed
in-memory
What you write.
Specific directory structure and components e.g., DESCRIPTION, an R/ directory.
source
bundled
binary
installed
in-memory
Package files compressed to single file.
Conventionally .tar.gz
source
bundled
binary
installed
in-memory
Standard package distribution
Platform specific: .tgz (Mac) .zip (Windows)
Package developers submit a bundle to CRAN; CRAN makes and distributes binaries
source
bundled
binary
installed
in-memory
A binary package thatβs been decompressed into a package library
source
bundled
binary
installed
in-memory
If a package is installed, library() makes its functions available by loading the package into memory and attaching it to the search path.
Not used when making a package; devtools::load_all() instead.
Be deliberate about where you create your package
Do not nest inside another RStudio project, R package or git repo.
π¬ Create a package:
β Creating 'C:/Users/er13/Desktop/mypackage/'
β Setting active project to 'C:/Users/er13/Desktop/mypackage'
β Creating 'R/'
β Writing 'DESCRIPTION'
Package: mypackage
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors\@R (parsed):
* First Last \<first.last\@example.com\> \[aut, cre\] (YOUR-ORCID-ID)
Description: What the package does (one paragraph).
License: \`use_mit_license()\`, \`use_gpl3_license()\` or friends to
pick a license
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
β Writing 'NAMESPACE'
β Writing 'mypackage.Rproj'
β Adding '.Rproj.user' to '.gitignore'
β Adding '\^mypackage\\\\.Rproj\$', '\^\\\\.Rproj\\\\.user\$' to '.Rbuildignore'
β Opening 'C:/Users/er13/Desktop/mypackage/' in new RStudio session
β Setting active project to '\<no active project\>'create_package()What happens when we run create_package()?
R will create a folder called mypackage which is a package and an RStudio project
restart R in the new project
create some infrastructure for your package
start the RStudio Build pane
create_package()What happens when we run create_package()?
mypackage.Rproj makes this directory an RStudio Project.
DESCRIPTION provides metadata about your package.
The R/ directory for .R files with function definitions.
NAMESPACE declares the functions your package exports and the functions it imports from other packages.
create_package()What happens when we run create_package()?
.Rbuildignore lists files that we need but that should not be included when building the R package from source.
.gitignore anticipates Git usage and ignores some standard, behind-the-scenes files created by R and RStudio.
Functions will go in an .R file.
Thereβs a usethis helper for adding .R files!
usethis::use_r() adds the file extension and saves in R/ folder
usethis::use_r()π¬ Create a new R file in your package called animal_sounds.R
π¬ Put the following code into your script:
In a normal script you might use:
but when building packages we use a devtools approach
Development workflow
devtools::load_all()π¬ Load package with devtools::load_all().
Loading mypackage
Test the animal_sounds() function in the console.
[1] "The dog goes woof!"
devtools::load_all()π¬ Change some tiny thing about your function - maybe the animal βsaysβ instead of βgoesβ?
π¬ Load with devtools::load_all() and test the updated function.
R CMD check is the gold standard for checking that an R package is in full working order.
It is a programme that is executed in the shell.
However, devtools has the check() function to allow you to run this without leaving your R session.
devtools::check()π¬ Check your package
devtools::check()You will get lots of output ending with:
ββ R CMD check results βββββββββββββββββββββββββββββββββββββ mypackage 0.0.0.9000 ββββ
Duration: 19.5s
β― checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'assertthat'
β― checking DESCRIPTION meta-information ... NOTE
Invalid licence file pointers: LICENSE
0 errors β | 1 warning β | 1 note βusethis helps out again! use_mit_license(), use_agpl_license(), use_ccby_license() etc
π¬ Add a MIT license1 - use your own name!
π¬ What files have appeared?
π¬ How has the DESCRIPTION file changed?
π¬ Run devtools::check() again. Has one of the warnings disappeared?
Metadata: The DESCRIPTION file β an overview of βwhatβs in this package?β
Object documentation: Documentation for each of the exported functions and datasets in the package, along with examples of usage
Vignettes: Long form documentation, generally discussing how to use a number of functions from the - package together and/or how a package fits into a larger ecosystem of packages
pkgdown sites: Websites for your package!
DESCRIPTION
Title: One line, title case, with no period. Fewer than 65 characters.
Version
Authors@R: βautβ means author, βcreβ means creator, βctbβ means contributor.
Description: One paragraph describing what the package does. Keep the width of the paragraph to 80 characters; indent subsequent lines with 4 spaces.
License
Encoding: How to encode text, use UTF-8 encoding.
LazyData: Use true to lazy-load data sets in the package.
DESCRIPTION
π¬ Add a title and description.
π¬ Add yourself as an author and creator.
Object documentation is what you see when you use ? or help() to find out more about a function or a dataset in a package.
Created by adding βRoxygen commentsβ to the function (\#')
Much of the work is done by the roxygen2 package called by βdevtoolsβ
Add roxygen comments to your .R files.
Run devtools::document() to convert roxygen comments to .Rd files.
Load the current version of the package with devtools::load_all()
Preview documentation with ?
Rinse and repeat until the documentation looks the way you want.
π¬ Open animal_sounds.R
π¬ Go to Code > Insert Roxygen Documentation
π¬ Fill in the documentation: Title, a brief description, define the two parameters, and finally, describe what the function returns
π¬ Save animal_sounds.R, run devtools::document() followed by devtools::load_all()
π¬ Preview the documentation with ?animal_sounds and edit your documentation if anything needs to be changed
π¬ Under @examples, add one example for using your function
π¬ Save animal_sounds.R, run devtools::document() followed by devtools::load_all()
π¬ Preview the documentation with ?animal_sounds and edit your documentation if anything needs to be changed
We have used a function from the assertthat package in a function in our package
But we havenβt declared that officially, we need to do that
usethis::use_package() is there for us again! It defaults to imports1
π¬ Use usethis::use_package() to add the assertthat package to Imports
π¬ How your DESCRIPTION file changed?
π¬ Run devtools::check() again. Has the warning disappeared?
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. 
devtools
devtools
DESCRIPTION, NAMESPACE, .Rbuildignore .gitignore
R/ directory for functionsusethis:use_r()
devtools::load_all()
devtools::check() to execute R CMD check
usethis::use_mit_license()
roxygen2 and devtools::document()
usethis::use_package()