Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active December 7, 2018 16:05
Show Gist options
  • Save benmarwick/57807d786d5dd666739e9a3c1d3fac6a to your computer and use it in GitHub Desktop.
Save benmarwick/57807d786d5dd666739e9a3c1d3fac6a to your computer and use it in GitHub Desktop.
Providing CRAN packages in low- or no-bandwidth environments using miniCRAN as described in https://software-carpentry.org/blog/2015/03/teaching-in-yangon.html
## For the user with a new installation of R.
# They get a USB stick with a directory containing an .RProj file,
# the workshop files, and the local_CRAN directory. They open
# the .RProj, which sets the working directory for RStudio, and then
# they open this file and run it, which installs pkgs from
# the local_CRAN directory on the USB stick.
# Specify list of packages to install
pkgs <- c('ggplot2', 'rmarkdown', 'knitr')
# Set location to store source files
local_CRAN <- paste0(getwd(), "/local_CRAN")
# do installation
install.packages(pkgs,
repos = paste0("file:///", local_CRAN),
type = "source")
# make packages available to this session
sapply(pkgs, require, character.only = TRUE)
############################################################
# Prepare a local repo of packages using miniCRAN
# for students to install offline. The workshop leader runs
# this script to prepare the local_CRAN directory. This
# script is not provided to the students
# We need to have a good internet connection for these steps
library("miniCRAN")
# Specify list of packages to download
pkgs <- c('stringr', 'devtools', 'ggplot2', 'dplyr', 'tidyr', 'rmarkdown', 'knitr', 'reshape2', 'gdata', 'xlsx')
# Make list of package URLs
revolution <- c(CRAN="http://cran.revolutionanalytics.com")
pkgList <- pkgDep(pkgs, repos=revolution, type="source" )
# Set location to store source files
local_CRAN <- paste0(getwd(), "/local_CRAN")
# Make repo for source and binaries, I just get source and win because I know
# no-one has OSX (too expensive!)
makeRepo(pkgList, path = local_CRAN, repos = revolution, type = "source")
makeRepo(pkgList, path = local_CRAN, repos = revolution, type = "win.binary")
# install...
install.packages(pkgs,
repos = paste0("file:///", local_CRAN),
type = "source")
# test to see if we can use them
sapply(pkgs, require, character.only = TRUE)
############################################################
@petbadger
Copy link

petbadger commented Dec 6, 2018

I think you can get away with one makeRepo command by doing type = c("source", "win.binary")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment