New R installation with your old library collection: a scripting solution
lib_path_file <- file(description = 'lib_path.txt',
open = 'r')
lib_path <- readLines(con = lib_path_file,
n = 1,
warn = F)
close(lib_path_file)
# obtaining the library folder names ----
inst_packgs <- list.files(lib_path)
# saving the library list as a table on the disc ----
write(inst_packgs,
file = 'installed_pcgs.txt')
if(sep) {
separator <- paste(rep('>', 40), collapse = '')
} else {
separator <- NULL
}
message(paste(msg_text, separator))
}
package_installer <- function(package_name_vec) {
## installs the packages from the list, if not already installed
## works with BiocInstaller
base_pcks <- unname(installed.packages()[, 'Package']) ## packages installed before starting the function
curr_installed <- base_pcks ## a list of the installed packages updated during the installation process
if(!'BiocManager' %in% base_pcks) {
inst_message('Installing BiocManager')
install.packages('BiocManager') ## installing the BiocManager, if not done before
}
bioc_available <- BiocManager::available() ## packages that can be installed by BiocManager
inst_result <- data.frame(Package = package_name_vec,
Installed = rep(NA, length(package_name_vec))) ## a data table holding the installation results
for(pckg_name in package_name_vec) {
inst_message(paste('Installing:', pckg_name))
if(!pckg_name %in% bioc_available) {
inst_result[inst_result[, 'Package'] == pckg_name, 'Installed'] <- 'Not available'
inst_message('Package unavailable', sep = F)
next
} else if(pckg_name %in% curr_installed){
inst_result[inst_result[, 'Package'] == pckg_name, 'Installed'] <- 'Already installed'
inst_message('Package already installed', sep = F)
next
} else {
BiocManager::install(pckg_name)
inst_result[inst_result[, 'Package'] == pckg_name, 'Installed'] <- 'Newly installed'
inst_message('Package successfully installed', sep = F)
}
}
return(inst_result)
}
The second function has one minor drawback: the dependencies installed along with the given package are listed in the result table as 'Already installed' - one may consider improving it, for my purposes the function was more than enough. The rest of the script is straight forward, in this case the list of old packages is provided in the text file:
pckg_lst_file <- file('installed_pcgs.txt',
open = 'r')
pckg_lst <- readLines(pckg_lst_file)
close(pckg_lst_file)
# installing the packages -----
inst_results <- package_installer(pckg_lst)
write.table(inst_results,
file = 'inst_results.txt',
sep = '\t')
# END ---
Comments
Post a Comment