2

I am wondering if there is a nice way to check of a package belongs to Bioconductor (because the installation is different than R CRAN package.

For example, I want to do something like that:

libraries <- c("ggplot2","BioBase")
check.libraries <- is.element(libraries, installed.packages()[, 1])==FALSE
libraries.to.install <- libraries[check.libraries]
if (length(libraries.to.install!=0)) {
  install.packages(libraries.to.install)
}

success <- sapply(libraries,require, quietly = FALSE,  character.only = TRUE)
if(length(success) != length(libraries)) {stop("A package failed to return a success in require() function.")}

This piece of code, checks if the libraries are installed if not are installed. But since Bioconductor Packages are installed in a different way, i.e: Biocmanager::install("Biobase") I want to do a condition. I checked for BiocCheck But I think it does not do the work.

3 Answers3

2

Not the smartest answer but you can prepare an exhaustive list of all packages once and maybe write it to csv so as to avoid doing this again and again. Downloading BioConductor packages code taken from here.

library(dplyr)
library(rvest)

CRANpackages <- available.packages() %>% 
                  as.data.frame() %>% 
                  select(Package) %>% 
                  mutate(source = 'CRAN')

url <- 'https://www.bioconductor.org/packages/release/bioc/'
biocPackages <- url %>% 
                  read_html() %>% 
                  html_table() %>%
                  .[[1]] %>%
                  select(Package) %>% 
                  mutate(source = 'BioConductor')

all_packages <- bind_rows(CRANpackages, biocPackages) 
rownames(all_packages) <- NULL

write.csv(all_packages, 'All_packages.csv', row.names = FALSE)

Now you can filter the packages that you want to check from this dataframe -

libraries <- c("ggplot2","Biobase")
result <- all_packages %>% filter(Package %in% libraries)
result

#  Package       source
#1 ggplot2         CRAN
#2 Biobase BioConductor

Get the packages to be installed from CRAN by result$Package[result$source == 'CRAN'] and by BioConductor as result$Package[result$source == 'BioConductor'] which can be passed to their respective functions.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I it is a good answer but I do not like the idea of create a csv, because I have several Projects, however I will try. And also I will try with tryCatch – Edmond Geraud Aguilar May 26 '21 at 10:07
0
libraries <- c("ggplot2", "Biobase")

if (!require("BiocManager")) install.packages("BiocManager")

#Checking if the package belongs to CRAN or to Bioconductor and installing them accordingly.

for(lib in libraries){
        if(!lib %in% installed.packages()){
            if(lib %in% available.packages()[,1]){
             install.packages(lib,dependencies=TRUE)
            }else {(BiocManager::install(lib))
            }}
        }

#Loading the libraries
sapply(libraries,require,character=TRUE)

I think this would help.

Nitesh Shriwash
  • 128
  • 1
  • 6
0

You can get a list of all available bioconductor packages, with:

BiocManager::available()

and as mentioned by others for CRAN: installed.packages(). see also: Managing bioconductor packages

Then you could write a for loop with an if-else statement to go over all the packages you have.

However, if you don't want to deal with this, I would look into package managers, which can handle different sources, e.g. GitHub, Bioconductor and CRAN. You could check out: r-lib/pak or my forked version, the latter allows you to use pak::pkg_load, which first checks whether your package is already installed, if not it will install it and then will try load it. pak allows you to give a list/vector of packages that you want to install and installs it, without specifying whether it is Github, CRAN etc.

An alternative could also be pacman, which does something very similar.

Joan
  • 30
  • 4