Skip to content

Instantly share code, notes, and snippets.

@mndrake
Created March 22, 2022 16:22
Show Gist options
  • Save mndrake/8fe1779a46e48489bd5eb9b1e372ed04 to your computer and use it in GitHub Desktop.
Save mndrake/8fe1779a46e48489bd5eb9b1e372ed04 to your computer and use it in GitHub Desktop.
miniCRAN Example
library(miniCRAN)
library(remotes)
# CRAN mirror to use (recommend checkpoint date for installed version of R https://mran.microsoft.com/timemachine)
cran_repo <- c(CRAN = "https://cran.microsoft.com/snapshot/2018-11-30")
# local path to create miniCRAN repo
miniCRAN_dir <- "/data/dataiku/miniCRAN"
# utility method to install from GitHub
addPackageGitHub <- function(repo, username = NULL, ref = "master", subdir = NULL, auth_token = remotes:::github_pat(), ...) {
packageName <- basename(repo)
exDir <- file.path(tempdir(), packageName)
if (dir.exists(exDir)) unlink(exDir, recursive = TRUE)
remote <- remotes:::github_remote(repo = repo, username = username, ref = ref, subdir = subdir, auth_token = auth_token)
tarFile <- remotes:::remote_download(remote)
#untar(tarFile, exdir = exDir)
unzip(tarFile, exdir = exDir)
file.rename(list.files(exDir, full.names = TRUE)[1], file.path(exDir, packageName))
## TODO: addLocalPackage() does not add the dependencies of the locally built package!
addLocalPackage(packageName, exDir, ..., build = TRUE)
}
# install github packages
addPackageGitHub('jcheng5/bubbles', path=miniCRAN_dir)
addPackageGitHub('hadley/shinySignals', path=miniCRAN_dir)
#addPackageGitHub('IRkernel/IRkernel', ref="0.8.6", path=miniCRAN_dir)
#addPackageGitHub('r-lib/filelock', path=miniCRAN_dir)
# install additional github packages
#addPackage(c("gtools", "RJSONIO"), path = miniCRAN_dir, repos = cran_repo, type=c("source"))
# CRAN mirror to use (recommend checkpoint date for installed version of R https://mran.microsoft.com/timemachine)
cran_repo <- c(CRAN = "https://cran.microsoft.com/snapshot/2018-11-30")
# local path to create miniCRAN repo
miniCRAN_dir <- "/data/dataiku/miniCRAN"
# user library
user_lib <- Sys.getenv("R_LIBS_USER")
# create local user library path if it does not exist
dir.create(path = user_lib, showWarnings = FALSE, recursive = TRUE)
# install required packages to setup miniCRAN if not installed
required_packages <- c("miniCRAN", "remotes", "devtools")
for (p in required_packages){
if (!(p %in% installed.packages())) install.packages(p, lib= user_lib, repos = cran_repo)
}
library(miniCRAN)
# create package database
pdb <- pkgAvail(repos = cran_repo, type="source")
# CRAN packages to install
pkgs <- c("shiny", "dplyr", "htmlwidgets", "digest", "bit", "shinydashboard", "gtools", "RJSONIO", "IRkernel", "filelock")
# dependencies to install
pkg_list <- pkgDep(pkgs, availPkgs = pdb)
# create miniCRAN repo
makeRepo(pkg_list, path=miniCRAN_dir, repos = cran_repo, type=c("source"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment