miniCRAN – developing internal CRAN Repositories

January 19, 2016
By

(This article was first published on Mango Solutions, and kindly contributed to R-bloggers)

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 already captured
                     , 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.

To leave a comment for the author, please follow the link and comment on their blog: Mango Solutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de









ODSC

CRC R books series













Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)