R Cheatsheet Devtools
R Cheatsheet Devtools
Package Structure
A package is a convention for organizing files into
directories.
Setup
Write code
Test
Document
Teach
Add data
Organize
CC0
MIT
GPL-2
No strings attached.
Test
tests/)
Setup
( "( $
DESCRIPTION)
Use
to store unit
tests thatyour
will inform
yousets
if
The$"tests/
DESCRIPTION
file describes
work and
your code
up how your package
willever
workbreaks.
with other packages.
CRAN
install.packages(type = "source")
CRAN
Internet
In memory
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().
R CMD install
github
devtools::install()
devtools::build()
devtools::install_github()
devtools::load_all()
Build & Reload (RStudio)
library()
Installed
Binary
Bundle
Source
Repository
install.packages()
On disk
library memory
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
Import packages that your package
Imports:
must have to work. R will install
dplyr (>= 0.4.0),
them when it installs your package.
ggvis (>= 0.2)
Suggest packages that are not very
Suggests:
essential to yours. Users can install
knitr (>= 0.1.0)
them manually, or not, as they like.
devtools::add_build_ignore("file")
Adds file to .Rbuildignore, a list of files that will not be included
when package is built.
RStudio is a trademark of RStudio, Inc. CC BY RStudio [email protected] 844-448-1212 rstudio.com
You amust
have
a DESCRIPTION
file testthat with
Add
tests/
directory
and import
%
% Adddevtools::use_testthat()
the packages that yours relies on with
Sets up package to use automated tests with
% devtools::use_package()
testthat
Adds a package to the Imports file (default) or
fieldcontext(),
(if second test(),
argument
"Suggests").
tests with
andisexpectations
% WriteSuggests
Suggests
your tests as .R files in tests/testthat/
% SaveImports
Import packages that your package
must have to work. R will install
them when it installs your package.
Workflow
Example test
Package:
1. Modify
yourmypackage
code or tests.
Title: Title of Package
2. Test
your code
with one of
Version:
0.1.0
context("Arithmetic")
Authors@R:
person("Hadley",
"Wickham", email =
devtools::test()
works",
{
"[email protected]", role test_that("Math
= c("aut", "cre",
"cst"))
Runs
all
tests
saved
in
expect_equal(1
+
1,
2)
Description: What the package does (one paragraph)
$ tests/.
expect_equal(1 + 2, 3)
Depends:
R (>= 3.1.0)
expect_equal(1 + 3, 4)
License:
GPL-2
Ctrl/Cmd + Shift + T
})
LazyData: true
(keyboard shortcut)
Imports:
Repeat
until (>=
all tests
pass
dplyr
0.4.0),
ggvis (>= 0.2)
expect_equal()
Suggests: is equal within small numerical tolerance?
knitr (>=
0.1.0)
expect_identical()
is exactly
equal?
3.
expect_match()
expect_output()
expect_message()
expect_warning()
expect_error()
expect_is()
expect_false()
returns FALSE?
expect_true()
returns TRUE?
Learn more at http://r-pkgs.had.co.nz devtools 1.6.1 Updated: 1/15
Document ( $ man/)
$ man/ contains the documentation for your functions, the help pages in your package.
%
%
%
Workflow
devtools::document()
Converts roxygen comments to .Rd files
and places them in $ man/. Also
assembles NAMESPACE.
4. Repeat
.Rd formatting tags
\emph{}
\strong{}
\code{}
\pkg{}
\code{\link{}}
\link{}
\link[package]{}
\linkS4class{}
\dontrun{}
\dontshow{}
\donttest{}
\email{}
\href{}
\url{}
\deqn{}
\eqn{}
\tabular{lcr}
\tab
\cr
@aliases
@concepts
@describeIn
@examples
@export
@family
@inheritParams
@keywords
@param
@rdname
@return
@section
@seealso
@format
@source
@include
@slot
@field
%
%
%
% 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 dataraw/. 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()
Teach ( $ vignettes/)
%
%
Workflow