Components of an R package

Developing R Packages

Jasmin Ludolf

Content Developer

The power of R packages

 

  • Share and reuse code across projects, business, and community

 

This course:

  • Create R packages from basics
  • Including documentation and testing

R package community art

Developing R Packages

R package components

Example package structure

1 Tree generated with https://tree.nathanfriend.io/
Developing R Packages

R package components

 

  • Functions
  • Data

Example package structure

Developing R Packages

R package components

 

  • Supporting files:
    • DESCRIPTION
    • NAMESPACE

Example package structure

Developing R Packages

R package components

 

  • R Markdown templates:
    • How to work with the package
  • R Markdown is used for creating dynamic reports

Example package structure

Developing R Packages

R package components

 

  • Documentation
    • Examples of how to use functions

Example package structure

Developing R Packages

R package components

 

  • Unit tests
    • Automated tests
    • Ensures functions and code work correctly

Example package structure

Developing R Packages

R package components

 

  • Vignettes
    • Long-form package tutorials

Example package structure

Developing R Packages

Core package functionality

At the very least, R packages need to include:

  • R directory
  • man directory
  • NAMESPACE file
  • DESCRIPTION file
Developing R Packages

R directory

 

  • Where package functions reside
  • Separate files for each component of the package
  • Include a file with the package name and details

 

R directory layout

Developing R Packages

man directory

 

  • manual directory
  • Documentation on package functions and objects

 

man directory layout

Developing R Packages

NAMESPACE

  • Shows functions imported from other packages
  • Lists functions available to users
# Generated by roxygen2: do not edit by hand
export(distance_converter)
export(temp_converter)

importFrom(tibble,tibble)
Developing R Packages

DESCRIPTION

  • Includes package metadata
Package: ourPackage
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R: 
    person("First", "Last", , "[email protected]", role = c("aut", "cre"),
           comment = c(ORCID = "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
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Depends: 
    R (>= 2.10)
LazyData: true
Developing R Packages

Let's practice!

Developing R Packages

Preparing Video For Download...