R/dependency-managers.R

Defines functions load_packages install_packages install_dependencies load_dependencies session_platform session_packages

Documented in install_dependencies install_packages load_dependencies load_packages session_packages session_platform

#' Load many packages to memory at once.
#'
#' @export

load_packages <- function(packages, quietly = FALSE) {
    for (package in packages) {
        library(package, character.only = TRUE, quietly = quietly)
    }
}

#' Install and update many packages at once.
#'
#' @export

install_packages <- function(packages, update, bioconductor_version) {

    # for checking github-formatted input
    if (!requireNamespace("stringr", quietly = TRUE)) {
        install.packages("stringr", quiet = TRUE, lib = .libPaths()[1])
    }

    # for installing the rest of the packages
    if (!requireNamespace("BiocManager", quietly = TRUE)) {
        install.packages("BiocManager", quiet = TRUE, lib = .libPaths()[1])
    }


    for (package in packages) {

        # re-format github input
        if (stringr::str_detect(package, "/")) {
            package_test <- stringr::str_extract(package, "[a-zA-Z]+$")
            package_install <- package
        } else {
            package_install <- package
            package_test <- package
        }

        if (!requireNamespace(package_test, quietly = TRUE)) {
            BiocManager::install(
                package_install,
                update = update,
                ask = FALSE,
                quiet = TRUE,
                verbose = FALSE,
                version = bioconductor_version,
                lib = .libPaths()[1]
            )
        }
    }

    # update all packages for sure
    if (update == update) {
        # update only the first 
        update.packages(
            ask = FALSE, 
            quiet = TRUE, 
            lib.loc = .libPaths()[1], 
            instlib = .libPaths()[1]
        )
    }
}

#' Install all dependencies of the TVS project.
#' 
#' A wrapper for a list of packages used in the project and a call to the tvsproject::install_packages().
#' 
#' @param bioconductor_version The version of the preferred bioconductor version (character).
#' @param update Whether old packages should be updated (logical).
#' 
#' @export

install_dependencies <- function(bioconductor_version = "3.9", update = TRUE) {

    packages_install <- c(
        # BiocManager::install is used to install the packages
        "BiocManager",
        "impute",
        "preprocessCore",
        "GO.db",
        "AnnotationDbi",
        "genefilter",
        "modeest",
        "here",
        "readxl",
        "lubridate",
        "janitor",
        "tidyverse",
        "WGCNA",
        "tidygraph",
        "ggraph",
        "magrittr",
        "knitr",
        "rmarkdown",
        "ggridges",
        "umap",
        "missForest",
        "GGally",
        "pcaMethods",
        # From GitHub, hence the format "<user>/<package>"
        "thomasp85/patchwork"
    )

    tvsproject::install_packages(
        packages_install, 
        bioconductor_version = bioconductor_version, 
        update = update
    )

}

#' Load all dependencies of the TVS analysis into memory.
#'
#' The function tries to minimize loading globally;
#' this is a fairly large project and namespace will suffer from
#' conflicts quickly. It also takes care of name conflicts:
#' the `lubridate` package needs to be loaded before `here`
#' because `here::here()` is needed rather than `lubridate::here()`.
#'
#' @export

load_dependencies <- function() {

    packages_load <- c(
        "lubridate",# dates
        "here",     # file paths
        "janitor",  # data cleaning tools
        "WGCNA",    # network analysis
        "tidygraph",# graph data manipulation
        "magrittr", # piping tools
        "tidyverse",# almost everything else
        "ggraph",   # visualization
        "ggridges", # visualization
        "patchwork"# visualization
    )

    tvsproject::load_packages(packages_load, quietly = FALSE)

}

#' Show relevant platform information of the current analysis session.
#'
#' This function is a small "prettifier" of `devtools::session_info` output.
#'
#' @importFrom dplyr "%>%"
#' @export

session_platform <- function() {

    session_platform <- devtools::session_info() %>%
        purrr::pluck("platform") %>%
        purrr::map_dfr(~ .x) %>%
        tidyr::gather("setting", "value") %>%
        dplyr::filter(setting %in% c("version", "os", "system"))

    return(session_platform)
}

#' Show relevant package information of the current analysis session.
#'
#' This function is a small "prettifier" of `devtools::session_info` output.
#'
#' @importFrom dplyr "%>%"
#' @export

session_packages <- function() {

   session_packages <- devtools::session_info() %>%
       purrr::pluck("packages") %>%
       tibble::as_tibble() %>%
       dplyr::select(package, contains("version"), source, attached)
       

   return(session_packages)
}
eteppo/tvs-project documentation built on Aug. 13, 2019, 8:53 a.m.