UNPKG

1.53 kBPlain TextView Raw
1#!/usr/bin/env Rscript
2
3# A R script which installs the packages (and their dependencies) listed
4# under Imports in a R package DESCRIPTION file.
5#
6# Sets up a local R package repository and installs from there and a
7# snapshot of CRAN at the date specified in the DESCRIPTION file
8#
9# It is necessary to go to the extent of creating a local R repository because `install.packages`
10# on a source package, *with* an external repo for dependencies i.e.
11#
12# install.packages("mypackage.tar.gz", type="source", repos="https://mran.microsoft.com/snapshot/2017-10-01/")
13#
14# does not work. For further discussion see:
15#
16# https://stackoverflow.com/questions/25017195/install-a-local-r-package-with-dependencies-from-cran-mirror
17# https://stackoverflow.com/questions/38732493/automatically-install-dependent-libraries-for-a-self-made-package
18
19# Read in the package description
20desc <- as.data.frame(read.dcf('DESCRIPTION'))
21
22# Create a temporary, local R package repository
23repo <- file.path(tempdir(), 'repo')
24src <- file.path(repo, 'src', 'contrib')
25dir.create(src, recursive=TRUE, showWarnings=FALSE)
26
27# Copy just the description file into it
28file.copy('DESCRIPTION', file.path(src, 'DESCRIPTION'))
29
30# Change to the repo
31setwd(src)
32
33# Build the package and repo package index
34tools::Rcmd(c("build", "."))
35tools::write_PACKAGES(".")
36
37# Install the package from the local repo
38install.packages(
39 desc$Package,
40 repos=c(
41 paste0("file:", repo),
42 paste0("https://mran.microsoft.com/snapshot/", desc$Date)
43 )
44)