Package creation and metadata

Explore your package libraries

Before we make a package, let’s look at where the packages you already have actually live:

.libPaths()

And at what is in them:

installed.packages()

That is a lot of information, so you may prefer to look at just a few columns:

installed.packages()[, c("Package", "Version", "LibPath")]

That is the raw material for the package we are about to build.

Lay the foundations

Load devtools and create your package infrastructure

library(devtools)                 # attaches usethis as well
create_package("~/work/libminer") # pick a place that makes sense to you!

Initialize this package as a git repo

use_git()

Attach devtools in all interactive sessions

We can load devtools automatically via our .Rprofile file so that it is always loaded during development

use_devtools()

Paste the contents of your clipboard in .Rprofile, and restart R.

If you choose not to do this, you’ll need to attach devtools everytime you restart R during your package development work:

library(devtools)

Write your first function

Open a new file in R/ to hold your code

use_r("lib_summary")

Write your function definition in this file

lib_summary <- function() {
  pkgs <- utils::installed.packages()
  pkg_tbl <- table(pkgs[, "LibPath"])
  pkg_df <- as.data.frame(pkg_tbl, stringsAsFactors = FALSE)
  names(pkg_df) <- c("Library", "n_packages")
  pkg_df
}

Load your package with load_all()

load_all()
TipLearn the load_all() keyboard shortcut

Positron also exposes this from the Command Palette as R: Load R Package, with the keyboard shortcut Cmd/Ctrl + Shift + L.

load_all() is the single most common thing you do while developing a package, so this shortcut is worth committing to muscle memory.

And test out our new function:

lib_summary()

Check your package

check()
TipLearn the check() keyboard shortcut

Positron also exposes this from the Command Palette as R: Check R Package, with the keyboard shortcut Cmd/Ctrl + Shift + E.

check() is your early warning system: run it often, so that when something does break you have a small set of recent changes to blame.

Choose a license, any license

use_mit_license()

Update DESCRIPTION with better metadata

Package: libminer
Title: Explore Your R Libraries
Version: 0.0.0.9000
Authors@R:
    person("Jane", "Doe",
           email = "jane.doe@something.com",
           role = c("aut", "cre"),
           comment = c(ORCID = "XXXX-XXXX-XXXX-XXXX"))
Description: Provides functions for learning about your R libraries, and the
    packages you have installed.

Check again

  • R: Check R Package from Positron Command Palette or
  • Cmd/Ctrl + Shift + E or
  • check() in R console (the command is better, though, because it runs in a clean terminal)

Use GitHub

use_github()