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.
- Install miniCRAN
# Set the repo for use throughout cran <- "https://cran.rstudio.org" # Install if(!require(miniCRAN)){ install.packages("miniCRAN", repos = cran) }
- 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)
- Make a CRAN structure
mCRAN <- "myminiCRAN" dir.create(mCRAN) # Use NULL to create an empty structure miniCRAN::makeRepo(NULL, mCRAN , repos = cran, type="source")
- Move my R packages to my miniCRAN
file.copy(list.files(tempdir,pattern = "*.tar.gz" , full.names = TRUE) , file.path(mCRAN, "src", "contrib"))
- Update the miniCRAN index
miniCRAN::updateRepoIndex(mCRAN)
- Get the file directory URI for correct referencing
uriCRAN <- paste0("file:///" , file.path(getwd(), mCRAN))
- 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)
- 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 already captured , deps = FALSE)
- 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.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.