Building reproducible analytical pipelines with R
Chapter 11 - Packaging your code

B. Rodrigues/Ricardo J. Serrano

2024-03-21

About me

Learning objectives:

  • How to convert the analysis that we have done so far into a package.
  • List benefits of developing a package.
  • Introduce {fusen} package to accelerate the process of building your package.
  • How to turn .Rmd files, including datasets, into a package.
  • Installing and sharing the package.
  • Conclusions
  • Q & A

  • Have you created an R package?

  • Have you created an R package?

  • Which R package(s) do you use frequently?

What is a package in R?

  • In R, the fundamental unit of shareable code is the package. Source: https://pixabay.com/

  • In simple terms, a package bundles together:

    • code
    • data
    • documentation
    • tests
  • Makes it easier to share with other users.

Benefits of developing an R package

  • Reproducibility

  • Makes it easier to reuse across projects

  • Facilitates collaboration

Introduction to {fusen}

Source: https://rawgit.com/rstudio/cheatsheets/main/package-development.pdf

Source: https://thinkr-open.github.io/fusen/

{fusen} process steps

if (!require("fusen")){
     install.packages("fusen")
     library(fusen)
}

Start an R session from your home (or Documents) directory and run the following:

fusen::create_fusen(path = "fusen.quickstart",
                    template = "minimal")

The create_fusen function creates:

  • a directory called fusen.quickstart in your Home directory
  • creates a fusen.quickstart.Rproj file
  • creates subfolders ‘dev/’ and ’R/

flat_minimal_package.Rmd file

0-dev_history.Rmd file

Example of DESCRIPTION file

Run code chunk named ‘description’ in an R console.

Run ‘development-inflate’ code chunk in flat_minimal_package.Rmd file.

Set pkgdown documentation website

usethis::use_pkgdown()
pkgdown::build_site()

Turning our Rmds into a package

  • Two .Rmd files: save_data.Rmd and analyse_data.Rmd
  • Only need one .Rmd file
  • Move two functions from analyse_data.Rmd to save_data.Rmd
  • If you’ve skipped the book previous chapters, fork this repository, and clone it to start afresh.
  • Switch to rmd branch (git checkout rmd) and create a branch called fusen (git checkout -b fusen)

Create a {fusen} flat template in a dev/ folder

Start a fresh R session inside the housing/ folder, and run this code:

fusen::create_fusen(path = ".",
                    template = "minimal",
                    overwrite = TRUE)

Move save_data.Rmd to dev/ folder

Make changes to save_data.Rmd

  • File should like this

  • Run description named chunk from the 0-dev-history.Rmd

  • Document save-data.Rmd using {roxygen2} -type comments (see example to document function-get_raw_data)

  • After the documentation changes, save_data.Rmd should look like this

Now, run the fusen::inflate()

fusen::inflate(flat_file = "dev/save_data.Rmd", vignette_name = "Nominal house prices in Luxembourg")

Including datasets

  • Go back to 0-dev_history.Rmd and look for the header “Including datasets”
  • 0-dev_history.Rmd
  • Run the chunk code wrapped in local
  • To document the datasets, we need to inflate data-doc.Rmd
  • Save the file in dev/ and inflate it

Installing and sharing the package

  • To install the package locally, run remotes::install_local()
  • Also, you can publish the code on GitHub

Conclusions

  • Developing an R package is a rite passage for any user to learn good coding and documentation practices.
  • For beginners, I would recommend to learn the traditional process (step-by-step) of developing R packages (usethis, devtools, roxygen2, etc.).
  • When you feel more comfortable, then I would recommend to use {fusen}.

Q & A