Remove software carpentries logo
[rnaseq-cwl-training.git] / bin / dependencies.R
1 install_required_packages <- function(lib = NULL, repos = getOption("repos", default = c(CRAN = "https://cran.rstudio.com/"))) {
2
3   if (is.null(lib)) {
4     lib <- .libPaths()
5   }
6
7   message("lib paths: ", paste(lib, collapse = ", "))
8   missing_pkgs <- setdiff(
9     c("rprojroot", "desc", "remotes", "renv"),
10     rownames(installed.packages(lib.loc = lib))
11   )
12   # The default installation of R will have "@CRAN@" as the default repository, which directs contrib.url() to either
13   # force the user to choose a mirror if interactive or fail if not. Since we are not interactve, we need to force the
14   # mirror here.
15   if ("@CRAN@" %in% repos) {
16     repos <- c(CRAN = "https://cran.rstudio.com/")
17   }
18
19   install.packages(missing_pkgs, lib = lib, repos = repos)
20
21 }
22
23 find_root <- function() {
24
25   cfg  <- rprojroot::has_file_pattern("^_config.y*ml$")
26   root <- rprojroot::find_root(cfg)
27
28   root
29 }
30
31 identify_dependencies <- function() {
32
33   root <- find_root()
34
35   required_pkgs <- unique(c(
36     ## Packages for episodes
37     renv::dependencies(file.path(root, "_episodes_rmd"), progress = FALSE, error = "ignore")$Package,
38     ## Packages for tools
39     renv::dependencies(file.path(root, "bin"), progress = FALSE, error = "ignore")$Package
40   ))
41
42   required_pkgs
43 }
44
45 create_description <- function(required_pkgs) {
46   d <- desc::description$new("!new")
47   lapply(required_pkgs, function(x) d$set_dep(x))
48   d$write("DESCRIPTION")
49 }
50
51 install_dependencies <- function(required_pkgs, ...) {
52
53   create_description(required_pkgs)
54   on.exit(file.remove("DESCRIPTION"))
55   remotes::install_deps(dependencies = TRUE, ...)
56
57   if (require("knitr") && packageVersion("knitr") < '1.9.19') {
58     stop("knitr must be version 1.9.20 or higher")
59   }
60
61 }