R
packaging
Published

October 7, 2023

R packages with pak

I started using pak install and manage R packages.

pak can be used to install all R packages used in a project.

The use case I am mainly interested in is installing and locking project packages to create reproducible environments. This can be accomplished with pak and renv. In a previous post, I discussed an approach with renv only.

renv is used to detect all packages used in a project and pac to create a (platform dependent) lockfile. This lockfile can be used to install the version pinned in the lock file later with pak without creating an renv environment.

Here is an example — not run:

# get packages used in project folder
deps <- unique(renv::dependencies()[["Package"]])

# install project packages
pak::pkg_install(deps, ask = FALSE, upgrade = FALSE)
# pak::lockfile_install()

# create lock files
pak::lockfile_create(deps)
# renv::snapshot(prompt = FALSE)

renv can be used to create a lock file as well. However, currently you have to use renv to install dependencies from an renv.lock file and need to create a new renv environment to reinstall the packages. There are plans to support installing packages from renv.lock with pak — see #343

Here are the R packages renv finds locally.

renv::dependencies()[["Package"]] |>
  unique() |>
  sort()
Finding R package dependencies ... Done!
[1] "renv"      "rmarkdown"

Python vs. R packaging tools

Pinning package versions in a lock file is an established workflow in Python with pip-tools.

In renv pinning package versions and creating local environments are too tightly coupled in my view. Often, I don’t want to create an environment for a project and need to document the used package versions only. In Docker or cloud platforms I don’t like to use local environments. In a local R install, working with the latest versions of R packages and freezing them is often sufficient for me.

I hope pak will provide the R packaging solution I am looking for.