rbioconductor

Is a way to check if a package belongs to Bioconductor or Cran?


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.


Solution

  • 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.