SlideShare a Scribd company logo
The contents of a package can be stored on disk as a:
• source - a directory with sub-directories (as above)
• bundle - a single compressed file (.tar.gz)
• binary - a single compressed file optimized for a specific
OS
Or installed into an R library (loaded into memory during an
R session) or archived online in a repository. Use the
functions below to move between these states.
install.packages() CRAN ○
install.packages(type = "source") CRAN ○
○ ○
R CMD install ○ ○
○ ○
devtools::install() ○
devtools::build() ○ ○
devtools::install_github() github ○
devtools::load_all() ○ ○
Build & Reload (RStudio) ○ ○ ○
library() ○ ○
Internet On disk library memory
Repository
Source
Bundle
Binary
Installed
Inmemory
Package Development
with devtools Cheat Sheet
Package Structure
A package is a convention for organizing files into
directories.
This sheet shows how to work with the 7 most common
parts of an R package:
Setup ( " DESCRIPTION)
The " DESCRIPTION file describes your work and sets
up how your package will work with other packages.
# Package
" DESCRIPTION
$ R/
$ tests/
$ man/
$ vignettes/
$ data/
" NAMESPACE
You must have a DESCRIPTION file
Add the packages that yours relies on with
devtools::use_package()
Adds a package to the Imports field (or Suggests
field (if second argument is "Suggests").
%
%
Setup
Write code
Test
Document
Teach
Add data
Organize
devtools::add_build_ignore("file")
Adds file to .Rbuildignore, a list of files that will not be included
when package is built.
%
Setup ( " DESCRIPTION)
The " DESCRIPTION file describes your work and sets
up how your package will work with other packages.
You must have a DESCRIPTION file
Add the packages that yours relies on with
devtools::use_package()
Adds a package to the Imports file (default) or
Suggests field (if second argument is "Suggests").
Package: mypackage
Title: Title of Package
Version: 0.1.0
Authors@R: person("Hadley", "Wickham", email =
"hadley@me.com", role = c("aut", "cre", "cst"))
Description: What the package does (one paragraph)
Depends: R (>= 3.1.0)
License: GPL-2
LazyData: true
Imports:
dplyr (>= 0.4.0),
ggvis (>= 0.2)
Suggests:
knitr (>= 0.1.0)
Import packages that your package
must have to work. R will install
them when it installs your package.
Suggest packages that re not really
essential to yours. Users can install
them manually, or not, as they like.
Imports Suggests
%
Package: mypackage
Title: Title of Package
Version: 0.1.0
Authors@R: person("Hadley", "Wickham", email =
"hadley@me.com", role = c("aut", "cre"))
Description: What the package does (one paragraph)
Depends: R (>= 3.1.0)
License: GPL-2
LazyData: true
Imports:
dplyr (>= 0.4.0),
ggvis (>= 0.2)
Suggests:
knitr (>= 0.1.0)
MIT license applies to
your code if re-shared.
MIT
Visit r-pkgs.had.co.nz for more
%
Use $ tests/ to store unit tests that will inform you if
your code ever breaks.
Test ( $ tests/)
Add a tests/ directory and import testthat with
devtools::use_testthat()
Sets up package to use automated tests with
testthat
Write tests with context(), test(), and expectations
Save your tests as .R files in tests/testthat/
1. Modify your code or tests.
2. Test your code with one of
devtools::test()
Runs all tests saved in
$ tests/.
Ctrl/Cmd + Shift + T
(keyboard shortcut)
3. Repeat until all tests pass
Workflow
%
%
expect_equal() is equal within small numerical tolerance?
expect_identical() is exactly equal?
expect_match() matches specified string or regular expression?
expect_output() prints specified output?
expect_message() displays specified message?
expect_warning() displays specified warning?
expect_error() throws specified error?
expect_is() output inherits from certain class?
expect_false() returns FALSE?
expect_true() returns TRUE?
context("Arithmetic")
test_that("Math works", {
expect_equal(1 + 1, 2)
expect_equal(1 + 2, 3)
expect_equal(1 + 3, 4)
})
Example test
Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15
Write code ( $ R/)
All of the R code in your package goes in $ R/. A package
with just an R/ directory is still a very useful package.
Create a new package project with
devtools::create("path/to/name")
Create a template to develop into a package.
Save your code in $ R/ as scripts (extension .R)
1. Edit your code.
2. Load your code with one of
devtools::load_all()
Re-loads all saved files in $ R/ into memory.
Ctrl/Cmd + Shift + L (keyboard shortcut)
Saves all open files then calls load_all().
3. Experiment in the console.
4. Repeat.
%
%
Workflow
• Use consistent style with r-pkgs.had.co.nz/r.html#style
• Click on a function and press F2 to open its definition
• Search for a function with Ctrl + .
RStudio® is a trademark of RStudio, Inc. • All rights reserved
info@rstudio.com • 844-448-1212 • rstudio.com
Suggest packages that are not very
essential to yours. Users can install
them manually, or not, as they like.
Import packages that your package
must have to work. R will install
them when it installs your package.
GPL-2 license applies to your
code, and all code anyone
bundles with it, if re-shared.
GPL-2
No strings attached.
CC0
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15
email{name@@foo.com}
href{url}{display}
url{url}
link[=dest]{display}
linkS4class{class}
code{link{function}}
code{link[package]{function}}
tabular{lcr}{
left tab centered tab right cr
cell tab cell tab cell cr
}
emph{italic text}
strong{bold text}
code{function(args)}
pkg{package}
dontrun{code}
dontshow{code}
donttest{code}
deqn{a + b (block)}
eqn{a + b (inline)}
Document ( $ man/)
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15
Organize ( " NAMESPACE)
The " NAMESPACE file helps you make your package
self-contained: it won’t interfere with other packages,
and other packages won’t interfere with it.
Export functions for users by placing @export in their
roxygen comments
Import objects from other packages with
package::object (recommended) or @import,
@importFrom, @importClassesFrom,
@importMethodsFrom (not always recommended)
%
%
Teach ( $ vignettes/)
$ man/ contains the documentation for your functions, the help pages in your package.
Add data ( $ data/)
The $ data/ directory allows you to include data with
your package.
Store data in one of data/, R/Sysdata.rda, inst/
extdata
Always use LazyData: true in your DESCRIPTION file.
Save data as .Rdata files (suggested)
%
%
devtools::use_data()
Adds a data object to data/
(R/Sysdata.rda if internal = TRUE)
devtools::use_data_raw()
Adds an R Script used to clean a data set to data-
raw/. Includes data-raw/ on .Rbuildignore.
%
Store data in
• data/ to make data available to package users
• R/sysdata.rda to keep data internal for use by your
functions.
• inst/extdata to make raw data available for loading and
parsing examples. Access this data with system.file()
1. Modify your code or tests.
2. Document your package (devtools::document())
3. Check NAMESPACE
4. Repeat until NAMESPACE is correct
Workflow
---
title: "Vignette Title"
author: "Vignette Author"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%VignetteIndexEntry{Vignette Title}
%VignetteEngine{knitr::rmarkdown}
usepackage[utf8]{inputenc}
---
$ vignettes/ holds documents that teach your users how
to solve real problems with your tools.
Create a $ vignettes/ directory and a template vignette with
devtools::use_vignette()
Adds template vignette as vignettes/my-vignette.Rmd.
Append YAML headers to your vignettes (like right)
Write the body of your vignettes in R Markdown
(rmarkdown.rstudio.com)
%
%
%
1. Add roxygen comments in your .R files
2. Convert roxygen comments into documentation
with one of
devtools::document()
Converts roxygen comments to .Rd files and
places them in $ man/. Builds NAMESPACE.
Ctrl/Cmd + Shift + D (Keyboard Shortcut)
3. Open help pages with ? to preview documentation
4. Repeat
Workflow
Use roxygen comments to document each
function beside its definition
Document the name of each exported data set
Include helpful examples for each function
%
%
%
The roxygen package
roxygen lets you write documentation inline in your .R files
with a shorthand syntax.
• Add roxygen documentation as comment lines that begin
with #’.
• Place comment lines directly above the code that defines
the object documented.
• Place a roxygen @ tag (right) after #’ to supply a specific
section of documentation.
• Untagged lines will be used to generate a title, description,
and details section (in that order)
#' Add together two numbers.
#'
#' @param x A number.
#' @param y A number.
#' @return The sum of code{x} and code{y}.
#' @examples
#' add(1, 1)
#' @export
add <- function(x, y) {
x + y
}
.Rd formatting tags
@aliases
@concepts
@describeIn
@examples
@export
@family
@inheritParams
@keywords
@param
@rdname
@return
@section
@seealso
@format
@source
@include
@slot
@field
Common roxygen tags
data
S4
RC
Submit your package
r-pkgs.had.co.nz/release.html

More Related Content

What's hot (20)

PDF
Presentation of sexy.rgtk
tuxette
 
PDF
Going beyond Code: Driving automation with data via Hiera
Dylan Cochran
 
PDF
Course 102: Lecture 3: Basic Concepts And Commands
Ahmed El-Arabawy
 
PDF
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
PDF
Gur1009
Cdiscount
 
PDF
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
PPTX
Postgresql Database Administration- Day4
PoguttuezhiniVP
 
DOCX
Big Data Analytics Lab File
Uttam Singh Chaudhary
 
PDF
服务框架: Thrift & PasteScript
Qiangning Hong
 
PDF
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
PPTX
Hadoop
Mukesh kumar
 
PDF
The Ring programming language version 1.5.1 book - Part 38 of 180
Mahmoud Samir Fayed
 
PDF
Ldap configuration documentation
Shree Niraula
 
PDF
DocBlox: your source matters @ #pfc11
Mike van Riel
 
PPT
2.4.1 use debian package management v2
Acácio Oliveira
 
PPTX
Cs267 hadoop programming
Kuldeep Dhole
 
PPTX
2019-01-29 - Demystifying Kotlin Coroutines
Eamonn Boyle
 
PDF
Apache Hive Hook
Minwoo Kim
 
ODP
Biopython
Karin Lagesen
 
PDF
Lambdas and Streams Master Class Part 2
José Paumard
 
Presentation of sexy.rgtk
tuxette
 
Going beyond Code: Driving automation with data via Hiera
Dylan Cochran
 
Course 102: Lecture 3: Basic Concepts And Commands
Ahmed El-Arabawy
 
Perl Programming - 03 Programming File
Danairat Thanabodithammachari
 
Gur1009
Cdiscount
 
Perl for System Automation - 01 Advanced File Processing
Danairat Thanabodithammachari
 
Postgresql Database Administration- Day4
PoguttuezhiniVP
 
Big Data Analytics Lab File
Uttam Singh Chaudhary
 
服务框架: Thrift & PasteScript
Qiangning Hong
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Hadoop
Mukesh kumar
 
The Ring programming language version 1.5.1 book - Part 38 of 180
Mahmoud Samir Fayed
 
Ldap configuration documentation
Shree Niraula
 
DocBlox: your source matters @ #pfc11
Mike van Riel
 
2.4.1 use debian package management v2
Acácio Oliveira
 
Cs267 hadoop programming
Kuldeep Dhole
 
2019-01-29 - Demystifying Kotlin Coroutines
Eamonn Boyle
 
Apache Hive Hook
Minwoo Kim
 
Biopython
Karin Lagesen
 
Lambdas and Streams Master Class Part 2
José Paumard
 

Similar to Devtools cheatsheet (20)

PPTX
AWSM packages and code script awsm1c2.pptx
kumawatrakeshrk76
 
PPTX
R Introduction
schamber
 
PDF
R Programming: Introduction To R Packages
Rsquared Academy
 
PDF
Reproducibility with R
Martin Jung
 
PPTX
DevTools Package Development
Sagar Deogirkar
 
PDF
1 Installing & getting started with R
naroranisha
 
PDF
1 installing & Getting Started with R
Dr Nisha Arora
 
PPTX
Data Handling in R language basic concepts.pptx
gameyug28
 
PPTX
Reproducible Computational Research in R
Samuel Bosch
 
PDF
Rmarkdown cheatsheet-2.0
Dieudonne Nahigombeye
 
PDF
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
PDF
Having Fun with Play
Clinton Dreisbach
 
DOC
Lex tool manual
Sami Said
 
PPTX
R sharing 101
Omnia Safaan
 
PPTX
Workshop presentation hands on r programming
Nimrita Koul
 
PDF
R package development, create package documentation isabella gollini
DataFest Tbilisi
 
RTF
Readme
rec2006
 
PDF
Assignment 1 MapReduce With Hadoop
Allison Thompson
 
PPTX
BUSINESS ANALYTICS WITH R SOFTWARE DIAST
HaritikaChhatwal1
 
PPT
r,rstats,r language,r packages
Ajay Ohri
 
AWSM packages and code script awsm1c2.pptx
kumawatrakeshrk76
 
R Introduction
schamber
 
R Programming: Introduction To R Packages
Rsquared Academy
 
Reproducibility with R
Martin Jung
 
DevTools Package Development
Sagar Deogirkar
 
1 Installing & getting started with R
naroranisha
 
1 installing & Getting Started with R
Dr Nisha Arora
 
Data Handling in R language basic concepts.pptx
gameyug28
 
Reproducible Computational Research in R
Samuel Bosch
 
Rmarkdown cheatsheet-2.0
Dieudonne Nahigombeye
 
Basics of R programming for analytics [Autosaved] (1).pdf
suanshu15
 
Having Fun with Play
Clinton Dreisbach
 
Lex tool manual
Sami Said
 
R sharing 101
Omnia Safaan
 
Workshop presentation hands on r programming
Nimrita Koul
 
R package development, create package documentation isabella gollini
DataFest Tbilisi
 
Readme
rec2006
 
Assignment 1 MapReduce With Hadoop
Allison Thompson
 
BUSINESS ANALYTICS WITH R SOFTWARE DIAST
HaritikaChhatwal1
 
r,rstats,r language,r packages
Ajay Ohri
 
Ad

More from Dr. Volkan OBAN (20)

PDF
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
 
PDF
Covid19py Python Package - Example
Dr. Volkan OBAN
 
PDF
Object detection with Python
Dr. Volkan OBAN
 
PDF
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
 
DOCX
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
 
DOCX
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
 
DOCX
k-means Clustering in Python
Dr. Volkan OBAN
 
DOCX
Naive Bayes Example using R
Dr. Volkan OBAN
 
DOCX
R forecasting Example
Dr. Volkan OBAN
 
DOCX
k-means Clustering and Custergram with R
Dr. Volkan OBAN
 
PDF
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
 
DOCX
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
PDF
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
PDF
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
 
PDF
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
PPTX
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
DOCX
R-ggplot2 package Examples
Dr. Volkan OBAN
 
DOCX
R Machine Learning packages( generally used)
Dr. Volkan OBAN
 
DOCX
treemap package in R and examples.
Dr. Volkan OBAN
 
Conference Paper:IMAGE PROCESSING AND OBJECT DETECTION APPLICATION: INSURANCE...
Dr. Volkan OBAN
 
Covid19py Python Package - Example
Dr. Volkan OBAN
 
Object detection with Python
Dr. Volkan OBAN
 
Python - Rastgele Orman(Random Forest) Parametreleri
Dr. Volkan OBAN
 
Linear Programming wi̇th R - Examples
Dr. Volkan OBAN
 
"optrees" package in R and examples.(optrees:finds optimal trees in weighted ...
Dr. Volkan OBAN
 
k-means Clustering in Python
Dr. Volkan OBAN
 
Naive Bayes Example using R
Dr. Volkan OBAN
 
R forecasting Example
Dr. Volkan OBAN
 
k-means Clustering and Custergram with R
Dr. Volkan OBAN
 
Data Science and its Relationship to Big Data and Data-Driven Decision Making
Dr. Volkan OBAN
 
Data Visualization with R.ggplot2 and its extensions examples.
Dr. Volkan OBAN
 
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
Python Pandas for Data Science cheatsheet
Dr. Volkan OBAN
 
Pandas,scipy,numpy cheatsheet
Dr. Volkan OBAN
 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
ReporteRs package in R. forming powerpoint documents-an example
Dr. Volkan OBAN
 
R-ggplot2 package Examples
Dr. Volkan OBAN
 
R Machine Learning packages( generally used)
Dr. Volkan OBAN
 
treemap package in R and examples.
Dr. Volkan OBAN
 
Ad

Recently uploaded (20)

PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
PDF
Choosing the Right Database for Indexing.pdf
Tamanna
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
Introduction to Artificial Intelligence.pptx
StarToon1
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PPTX
Climate Action.pptx action plan for climate
justfortalabat
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
PDF
MusicVideoProjectRubric Animation production music video.pdf
ALBERTIANCASUGA
 
PPTX
AI Project Cycle and Ethical Frameworks.pptx
RiddhimaVarshney1
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PDF
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
PPTX
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
PPTX
fashion industry boom.pptx an economics project
TGMPandeyji
 
PPTX
TSM_08_0811111111111111111111111111111111111111111111111
csomonasteriomoscow
 
PPT
1 DATALINK CONTROL and it's applications
karunanidhilithesh
 
DOCX
AI/ML Applications in Financial domain projects
Rituparna De
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
Choosing the Right Database for Indexing.pdf
Tamanna
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
Introduction to Artificial Intelligence.pptx
StarToon1
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Climate Action.pptx action plan for climate
justfortalabat
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
MusicVideoProjectRubric Animation production music video.pdf
ALBERTIANCASUGA
 
AI Project Cycle and Ethical Frameworks.pptx
RiddhimaVarshney1
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
AUDITABILITY & COMPLIANCE OF AI SYSTEMS IN HEALTHCARE
GAHI Youssef
 
Exploring Multilingual Embeddings for Italian Semantic Search: A Pretrained a...
Sease
 
fashion industry boom.pptx an economics project
TGMPandeyji
 
TSM_08_0811111111111111111111111111111111111111111111111
csomonasteriomoscow
 
1 DATALINK CONTROL and it's applications
karunanidhilithesh
 
AI/ML Applications in Financial domain projects
Rituparna De
 

Devtools cheatsheet

  • 1. The contents of a package can be stored on disk as a: • source - a directory with sub-directories (as above) • bundle - a single compressed file (.tar.gz) • binary - a single compressed file optimized for a specific OS Or installed into an R library (loaded into memory during an R session) or archived online in a repository. Use the functions below to move between these states. install.packages() CRAN ○ install.packages(type = "source") CRAN ○ ○ ○ R CMD install ○ ○ ○ ○ devtools::install() ○ devtools::build() ○ ○ devtools::install_github() github ○ devtools::load_all() ○ ○ Build & Reload (RStudio) ○ ○ ○ library() ○ ○ Internet On disk library memory Repository Source Bundle Binary Installed Inmemory Package Development with devtools Cheat Sheet Package Structure A package is a convention for organizing files into directories. This sheet shows how to work with the 7 most common parts of an R package: Setup ( " DESCRIPTION) The " DESCRIPTION file describes your work and sets up how your package will work with other packages. # Package " DESCRIPTION $ R/ $ tests/ $ man/ $ vignettes/ $ data/ " NAMESPACE You must have a DESCRIPTION file Add the packages that yours relies on with devtools::use_package() Adds a package to the Imports field (or Suggests field (if second argument is "Suggests"). % % Setup Write code Test Document Teach Add data Organize devtools::add_build_ignore("file") Adds file to .Rbuildignore, a list of files that will not be included when package is built. % Setup ( " DESCRIPTION) The " DESCRIPTION file describes your work and sets up how your package will work with other packages. You must have a DESCRIPTION file Add the packages that yours relies on with devtools::use_package() Adds a package to the Imports file (default) or Suggests field (if second argument is "Suggests"). Package: mypackage Title: Title of Package Version: 0.1.0 Authors@R: person("Hadley", "Wickham", email = "[email protected]", role = c("aut", "cre", "cst")) Description: What the package does (one paragraph) Depends: R (>= 3.1.0) License: GPL-2 LazyData: true Imports: dplyr (>= 0.4.0), ggvis (>= 0.2) Suggests: knitr (>= 0.1.0) Import packages that your package must have to work. R will install them when it installs your package. Suggest packages that re not really essential to yours. Users can install them manually, or not, as they like. Imports Suggests % Package: mypackage Title: Title of Package Version: 0.1.0 Authors@R: person("Hadley", "Wickham", email = "[email protected]", role = c("aut", "cre")) Description: What the package does (one paragraph) Depends: R (>= 3.1.0) License: GPL-2 LazyData: true Imports: dplyr (>= 0.4.0), ggvis (>= 0.2) Suggests: knitr (>= 0.1.0) MIT license applies to your code if re-shared. MIT Visit r-pkgs.had.co.nz for more % Use $ tests/ to store unit tests that will inform you if your code ever breaks. Test ( $ tests/) Add a tests/ directory and import testthat with devtools::use_testthat() Sets up package to use automated tests with testthat Write tests with context(), test(), and expectations Save your tests as .R files in tests/testthat/ 1. Modify your code or tests. 2. Test your code with one of devtools::test() Runs all tests saved in $ tests/. Ctrl/Cmd + Shift + T (keyboard shortcut) 3. Repeat until all tests pass Workflow % % expect_equal() is equal within small numerical tolerance? expect_identical() is exactly equal? expect_match() matches specified string or regular expression? expect_output() prints specified output? expect_message() displays specified message? expect_warning() displays specified warning? expect_error() throws specified error? expect_is() output inherits from certain class? expect_false() returns FALSE? expect_true() returns TRUE? context("Arithmetic") test_that("Math works", { expect_equal(1 + 1, 2) expect_equal(1 + 2, 3) expect_equal(1 + 3, 4) }) Example test Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15 Write code ( $ R/) All of the R code in your package goes in $ R/. A package with just an R/ directory is still a very useful package. Create a new package project with devtools::create("path/to/name") Create a template to develop into a package. Save your code in $ R/ as scripts (extension .R) 1. Edit your code. 2. Load your code with one of devtools::load_all() Re-loads all saved files in $ R/ into memory. Ctrl/Cmd + Shift + L (keyboard shortcut) Saves all open files then calls load_all(). 3. Experiment in the console. 4. Repeat. % % Workflow • Use consistent style with r-pkgs.had.co.nz/r.html#style • Click on a function and press F2 to open its definition • Search for a function with Ctrl + . RStudio® is a trademark of RStudio, Inc. • All rights reserved [email protected] • 844-448-1212 • rstudio.com Suggest packages that are not very essential to yours. Users can install them manually, or not, as they like. Import packages that your package must have to work. R will install them when it installs your package. GPL-2 license applies to your code, and all code anyone bundles with it, if re-shared. GPL-2 No strings attached. CC0 RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15
  • 2. email{name@@foo.com} href{url}{display} url{url} link[=dest]{display} linkS4class{class} code{link{function}} code{link[package]{function}} tabular{lcr}{ left tab centered tab right cr cell tab cell tab cell cr } emph{italic text} strong{bold text} code{function(args)} pkg{package} dontrun{code} dontshow{code} donttest{code} deqn{a + b (block)} eqn{a + b (inline)} Document ( $ man/) RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • [email protected] • 844-448-1212 • rstudio.com Learn more at http://r-pkgs.had.co.nz • devtools 1.6.1 • Updated: 1/15 Organize ( " NAMESPACE) The " NAMESPACE file helps you make your package self-contained: it won’t interfere with other packages, and other packages won’t interfere with it. Export functions for users by placing @export in their roxygen comments Import objects from other packages with package::object (recommended) or @import, @importFrom, @importClassesFrom, @importMethodsFrom (not always recommended) % % Teach ( $ vignettes/) $ man/ contains the documentation for your functions, the help pages in your package. Add data ( $ data/) The $ data/ directory allows you to include data with your package. Store data in one of data/, R/Sysdata.rda, inst/ extdata Always use LazyData: true in your DESCRIPTION file. Save data as .Rdata files (suggested) % % devtools::use_data() Adds a data object to data/ (R/Sysdata.rda if internal = TRUE) devtools::use_data_raw() Adds an R Script used to clean a data set to data- raw/. Includes data-raw/ on .Rbuildignore. % Store data in • data/ to make data available to package users • R/sysdata.rda to keep data internal for use by your functions. • inst/extdata to make raw data available for loading and parsing examples. Access this data with system.file() 1. Modify your code or tests. 2. Document your package (devtools::document()) 3. Check NAMESPACE 4. Repeat until NAMESPACE is correct Workflow --- title: "Vignette Title" author: "Vignette Author" date: "`r Sys.Date()`" output: rmarkdown::html_vignette vignette: > %VignetteIndexEntry{Vignette Title} %VignetteEngine{knitr::rmarkdown} usepackage[utf8]{inputenc} --- $ vignettes/ holds documents that teach your users how to solve real problems with your tools. Create a $ vignettes/ directory and a template vignette with devtools::use_vignette() Adds template vignette as vignettes/my-vignette.Rmd. Append YAML headers to your vignettes (like right) Write the body of your vignettes in R Markdown (rmarkdown.rstudio.com) % % % 1. Add roxygen comments in your .R files 2. Convert roxygen comments into documentation with one of devtools::document() Converts roxygen comments to .Rd files and places them in $ man/. Builds NAMESPACE. Ctrl/Cmd + Shift + D (Keyboard Shortcut) 3. Open help pages with ? to preview documentation 4. Repeat Workflow Use roxygen comments to document each function beside its definition Document the name of each exported data set Include helpful examples for each function % % % The roxygen package roxygen lets you write documentation inline in your .R files with a shorthand syntax. • Add roxygen documentation as comment lines that begin with #’. • Place comment lines directly above the code that defines the object documented. • Place a roxygen @ tag (right) after #’ to supply a specific section of documentation. • Untagged lines will be used to generate a title, description, and details section (in that order) #' Add together two numbers. #' #' @param x A number. #' @param y A number. #' @return The sum of code{x} and code{y}. #' @examples #' add(1, 1) #' @export add <- function(x, y) { x + y } .Rd formatting tags @aliases @concepts @describeIn @examples @export @family @inheritParams @keywords @param @rdname @return @section @seealso @format @source @include @slot @field Common roxygen tags data S4 RC Submit your package r-pkgs.had.co.nz/release.html