R/restore_packages.R

Defines functions restore_packages

Documented in restore_packages

#' @title Restores packages installed from a .rda file.
#'
#' @description Installs the packages contained in a .rda file. This file can be generated by the \code{\link{store_packages}} function of this package.
#'
#' @param rda_file Path to the .rda from which get the information for the packages to re-install.
#'
#' @return If it was not possible to re-install al packages, the list of not restored packages will be returned.
#'
#' @export restore_packages


restore_packages = function(rda_file) { #BEGIN
  #-----------------------------#
  # Check if Rseb is up-to-date #
  Rseb::actualize(update = F, verbose = F)   #
  #-----------------------------#


  load(rda_file) #creates a variable called 'installedpackages'

  #obtain the list of not already installed packages
  installed = row.names(installed.packages())
  to.install = installedpackages[!(installedpackages %in% installed)]

  #installs BioConductor if not installed
  if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager", dependencies = TRUE)

  #install githubinstall if not installed
  if (!requireNamespace("githubinstall", quietly = TRUE))
    install.packages("githubinstall", dependencies = TRUE)

  #install not already installed packages
  if (length(to.install) > 0) {
    for (i in 1:length(to.install)) {
      if(!require(to.install[i]) & !(is.na(to.install[i]))){
        install.packages(to.install[i], dependencies = TRUE)}
    }
  }

  #obtain list of still not installed packages, usally the BioConductor ones
  new.installed = row.names(installed.packages())
  BioC.to.install = installedpackages[!(installedpackages %in% new.installed)]

  #install all BioConductor packages
  if (length(BioC.to.install) > 0) {
    for (i in 1:length(BioC.to.install)) {
      if(!require(BioC.to.install[i]) & !(is.na(BioC.to.install[i]))){
        BiocManager::install(BioC.to.install[i])}
    }
  }

  update.packages(old.packages())

  new.installed = row.names(installed.packages())
  GH.to.install = installedpackages[!(installedpackages %in% new.installed)]

  # install all GitHub packages
  if (length(GH.to.install) > 0) {
    for (i in 1:length(BioC.to.install)) {
      if(!require(GH.to.install[i]) & !(is.na(GH.to.install[i]))){
        githubinstall::install(GH.to.install[i])}
    }
  }

  update.packages(old.packages())

  new.installed = row.names(installed.packages())
  still.to.install = installedpackages[!(installedpackages %in% new.installed)]

  # print not installed packages if present
  if (length(still.to.install) != 0) {
    warnings("It has not been possible to install the following packages")
    return(still.to.install)}
  else {
    message("All packages have been restored and updated!")}
} #END
sebastian-gregoricchio/Rseb documentation built on Sept. 4, 2024, 1:59 p.m.