Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

by Steph Locke

Today, I needed to work on a package that had numerous dependencies on internal packages and ones from CRAN. To be able to handle dependencies in the installation process, I needed something like CRAN so that install.packages() woul work correctly. We have an internal CRAN but I wanted to make one specific to this set of packages. Our early guidance, produced by Greg back in 2014 used a number of custom functions and manual folder structure creation. It worked but it required effort. Since then Revolution Analytics have developed miniCRAN, which was designed to make developing internal CRAN repositories a breeze.

It’s been really helpful for developing an internal repository for working on this project so I wanted to show others how easy it is too. In fact, my entire minimum reproducible example is just nine steps!

For this to be reproducible, I’ll be using the V8 package as my “internal” package and I’ll also mimic having one of it’s dependencies as a local .tar.gz file also.

1. Install miniCRAN
# Set the repo for use throughout
cran <- "https://cran.rstudio.org"

# Install
if(!require(miniCRAN)){
install.packages("miniCRAN", repos = cran)
}
1. Mimic having local R packages
library(miniCRAN)

# Config variables
tempdir <- tempdir()
pkg <- "V8"
pkgs <- c(pkg, "curl")

# Doesn't use miniCRAN::addPackage as it'd get all the deps
dir.create(tempdir)
utils::download.packages(pkgs, tempdir, repos= cran)
1. Make a CRAN structure
mCRAN <- "myminiCRAN"
dir.create(mCRAN)
# Use NULL to create an empty structure
miniCRAN::makeRepo(NULL, mCRAN
, repos = cran, type="source")
1. Move my R packages to my miniCRAN
file.copy(list.files(tempdir,pattern = "*.tar.gz"
, full.names = TRUE)
, file.path(mCRAN, "src", "contrib"))
1. Update the miniCRAN index
miniCRAN::updateRepoIndex(mCRAN)
1. Get the file directory URI for correct referencing
uriCRAN <- paste0("file:///"
, file.path(getwd(), mCRAN))
1. Identify all the dependencies for a package [Only need this step to get all dependencies into miniCRAN from CRAN]
deps <- miniCRAN::pkgDep(pkg
, repos = uriCRAN
, suggests = FALSE)
# Exclude locally available packages
deps <- setdiff(deps, pkgs)
1. Get all available dependencies from CRAN [Only need this step to get all dependencies into miniCRAN]
miniCRAN::addPackage(deps, mCRAN
, repos = cran
, writePACKAGES = TRUE
, deps = FALSE)
1. Install the internal package
install.packages(pkg, repos = uriCRAN
, type="source")

I hope this has helped show you a simple workflow for building your own internal CRAN. The code is available as a gist for download.