Skip to content

Commit

Permalink
update R dependency search; Allow Bioconductor packages (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkamvar authored May 21, 2021
1 parent e1f7f91 commit da77111
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ jobs:

- name: Install needed packages
if: steps.check-rmd.outputs.count != 0
working-directory: lesson
run: |
packages = setdiff(c('remotes', 'rprojroot', 'renv', 'desc', 'rmarkdown', 'knitr'), rownames(installed.packages()))
install.packages(packages, repo="https://cran.rstudio.com/")
source('bin/dependencies.R')
install_required_packages(.libPaths()[1])
shell: Rscript {0}

- name: Query dependencies
Expand All @@ -149,13 +150,14 @@ jobs:
source('bin/dependencies.R')
deps <- identify_dependencies()
create_description(deps)
use_bioc_repos()
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Cache R packages
- name: Restore Package Cache
if: runner.os != 'Windows' && steps.check-rmd.outputs.count != 0
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand All @@ -172,7 +174,7 @@ jobs:
run: |
while read -r cmd
do
eval sudo $cmd
eval sudo $cmd || echo "Nothing to update"
done < <(Rscript -e 'cat(remotes::system_requirements("ubuntu", "20.04"), sep = "\n")')
- run: make site
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ jobs:
with:
r-version: 'release'

- name: Cache R packages
- name: Restore R Cache
if: steps.check-rmd.outputs.count != 0
uses: actions/cache@v1
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
Expand All @@ -63,8 +63,8 @@ jobs:
- name: Install needed packages
if: steps.check-rmd.outputs.count != 0
run: |
packages = setdiff(c('remotes', 'rprojroot', 'renv', 'desc', 'rmarkdown', 'knitr'), rownames(installed.packages()))
install.packages(packages, repo="https://cran.rstudio.com/")
source('bin/dependencies.R')
install_required_packages()
shell: Rscript {0}

- name: Query dependencies
Expand All @@ -73,6 +73,7 @@ jobs:
source('bin/dependencies.R')
deps <- identify_dependencies()
create_description(deps)
use_bioc_repos()
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
Expand All @@ -83,7 +84,7 @@ jobs:
run: |
while read -r cmd
do
eval sudo $cmd
eval sudo $cmd || echo "Nothing to update"
done < <(Rscript -e 'cat(remotes::system_requirements("ubuntu", "20.04"), sep = "\n")')
- name: Render the markdown and confirm that the site can be built
Expand Down
63 changes: 54 additions & 9 deletions bin/dependencies.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
install_required_packages <- function(lib = NULL, repos = getOption("repos", default = c(CRAN = "https://cran.rstudio.com/"))) {

if (is.null(lib)) {
lib <- .libPaths()
lib <- .libPaths()[[1]]
}

message("lib paths: ", paste(lib, collapse = ", "))
required_pkgs <- c("rprojroot", "desc", "remotes", "renv")
# Note: RMarkdown is needed for renv to detect packages in Rmd documents.
required_pkgs <- c("rprojroot", "desc", "remotes", "renv", "BiocManager", "rmarkdown")
installed_pkgs <- rownames(installed.packages(lib.loc = lib))
missing_pkgs <- setdiff(required_pkgs, installed_pkgs)

# The default installation of R will have "@CRAN@" as the default repository, which directs contrib.url() to either
# force the user to choose a mirror if interactive or fail if not. Since we are not interactve, we need to force the
# mirror here.
# The default installation of R will have "@CRAN@" as the default repository,
# which directs contrib.url() to either force the user to choose a mirror if
# interactive or fail if not. Since we are not interactve, we need to force
# the mirror here.
if ("@CRAN@" %in% repos) {
repos <- c(CRAN = "https://cran.rstudio.com/")
}
Expand All @@ -29,30 +31,73 @@ find_root <- function() {
root
}

# set the BiocManager repositories and return a function that resets the default
# repositories.
#
# @example
# bioc_repos_example <- function() {
# message("User repos")
# as.data.frame(getOption("repos"))
# reset_repos <- use_bioc_repos()
# on.exit(reset_repos())
# message("Bioc repos")
# as.data.frame(getOption("repos"))
# }
# bioc_repos_example()
# as.data.frame(getOption("repos")
use_bioc_repos <- function() {
repos <- getOption("repos")
suppressMessages(options(repos = BiocManager::repositories()))
function() {
options(repos = repos)
}
}

identify_dependencies <- function() {

root <- find_root()

reset_repos <- use_bioc_repos()
on.exit(reset_repos(), add = TRUE)
eps <- file.path(root, "_episodes_rmd")
bin <- file.path(root, "bin")

required_pkgs <- unique(c(
## Packages for episodes
renv::dependencies(file.path(root, "_episodes_rmd"), progress = FALSE, error = "ignore")$Package,
renv::dependencies(eps, progress = FALSE, error = "ignored")$Package,
## Packages for tools
renv::dependencies(file.path(root, "bin"), progress = FALSE, error = "ignore")$Package
renv::dependencies(bin, progress = FALSE, error = "ignored")$Package
))

required_pkgs
}

create_description <- function(required_pkgs) {
d <- desc::description$new("!new")
lapply(required_pkgs, function(x) d$set_dep(x))
d$set_deps(data.frame(type = "Imports", package = required_pkgs, version = "*"))
d$write("DESCRIPTION")
# We have to write the description twice to get the hidden dependencies
# because renv only considers explicit dependencies.
#
# This is needed because some of the hidden dependencis will require system
# libraries to be configured.
suppressMessages(repo <- BiocManager::repositories())
deps <- remotes::dev_package_deps(dependencies = TRUE, repos = repo)
deps <- deps$package[deps$diff < 0]
if (length(deps)) {
# only create new DESCRIPTION file if there are dependencies to install
d$set_deps(data.frame(type = "Imports", package = deps, version = "*"))
d$write("DESCRIPTION")
}
}

install_dependencies <- function(required_pkgs, ...) {

reset_repos <- use_bioc_repos()
on.exit(reset_repos(), add = TRUE)

create_description(required_pkgs)
on.exit(file.remove("DESCRIPTION"))
on.exit(file.remove("DESCRIPTION"), add = TRUE)
remotes::install_deps(dependencies = TRUE, ...)

if (require("knitr") && packageVersion("knitr") < '1.9.19') {
Expand Down

0 comments on commit da77111

Please sign in to comment.