Intro
Why to write a package? Packages provide an easy way to share code with others or use it later, saving time by organizing the code and projects in standardized way.
Philosophy: anything that can be automated, should be automated.
Prerequisites: install.packages(c("devtools", "roxygen2", "testthat", "knitr"))
Basic workflow
create package either via
devtools::create(path)
or via RStudio–>new Project–>new R packagein case git should be used:
devtools::use_git()
editing functions:
usethis::use_r("functionname")
to create a new function script, naming the file after the function. New.R
-files for each user-facing function in the package. After adding more functions, these might be grouped.create
roxygen2
-skeleton to provide information and document functions via'Ctrl + Alt + Shift + R'
devtools::load_all()
to load the created functions underR/
subdirectoryusethis::use_package()
to include functions from other packages (adding them to theImports
field ofDESCRIPTION)
. The function can be used viapackagename::fun()
(git-commit after editing a function)
devtools::rename_files("old_name", "new_name")
to update the function name in files –> Don’t forget to update test-files, too!
check that an R package is in full working order with
devtools::check()
or'Ctrl + Shift + E'
, providing a convenient way to run this without leaving the R sessioncreate testing infrastructure via
usethis::use_testthat()
usethis::use_test("function_name")
to create a test-file for a specific functiondevtools::test()
or'Ctrl + Shift + T'
to run test
pick license, e.g. via
usethis::use_mit_license()
, creatingLICENSE
andLICENSE.md
-filescreate and update documentation via
devtools::document()
use_readme_rmd()
initializes a basic, executableREADME.Rmd
file for the github page to describe the purpose of the package, providing installation instructions, and showing a bit of usage.build_readme()
renders the fileInstallation:
devtools::check()
again, then install package into library viadevtools::install()
Publish:
devtools::build()
converts package folder/project into single bundled file
Structure
DESCRIPTION
provides metadata about the packageNAMESPACE
declares the functions the package exports for external use and the external functions your package imports from other packages. is automatically edited when usingroxygen2
.Rbuildignore
lists files that are needed to be around but should not be included when building the R package from source.Rproj.user
- directory used internally by RStudio.gitignore
anticipates Git usage and tells Git to ignore some standard, behind-the-scenes files created by R and RStudiotests/
directory in which the testing framework is placed, containing specific tests for the functions
Main functions during development
These functions setup parts of the package and are typically called once per package:
These functions are called on a regular basis, as adding functions and tests or taking on dependencies:
These functions are called multiple times per day or per hour, during development:
Main shortcuts
Ctrl + Shift + T
- rundevtools::test()
Ctrl + Shift + E
- rundevtools::check()
Ctrl + Shift + Alt + R
- create roxygen2 skeleton
Package states
source - directory of files with specific package structure
bundled - compressed into single file using extension
.tar.gz
binary - single platform-specific file
installed - binary package that’s been compressed into a package library
in-memory - package loaded into memory