Search code examples
rbioconductor

Can you silently install Bioconductor packages?


I'm writing a pipeline in Snakemake that calls an R script. This R script has its own environment, with r-base, r-ggplot2 and r-biocmanager in it. I also need the package ggbio that can be installed with biocmanager. I want to silently install this package when the script is called because I don't want it to flood my terminal. Is there a way to do this? I have this right now, but this still outputs instalment information to the terminal:

if(!require(ggbio, quietly=TRUE)){  # if the package is not there, install it
  BiocManager::install("ggbio")
  library(ggbio)
}
...

I also tried this which I saw here:

if(!suppressMessages(suppressWarnings(require(ggbio, quietly=TRUE)))){
  BiocManager::install("ggbio")
  library(ggbio)
}

# OR
if(!require(ggbio, quietly=TRUE)){
  suppressMessages(suppressWarnings(BiocManager::install("ggbio")))
  library(ggbio)
}
...

But this also still outputs to the terminal. There also doesn't seem to be a way to give an argument like quietly=TRUE to BiocManager::install("ggbio").

EDIT: invisible(), capture.output() and sink() as suggested here also doesn't work:

# invisible(capture.output()) around install
if(!require(ggbio, quietly=TRUE)){
  invisible(capture.output(BiocManager::install("ggbio")))  # doesn't work
}

# invisible(capture.output()) around entire if.
invisible(capture.output(if(!require(ggbio, quietly=TRUE)){  # doesn't work
  BiocManager::install("ggbio")
}))

# sink() (Linux)
sink("/dev/null")  # doesn't work
if(!require(ggbio, quietly=TRUE)){
  BiocManager::install("ggbio")
}
sink()

# only invisible()
if(!require(ggbio, quietly=TRUE)){
  invisible(BiocManager::install("ggbio"))  # doesn't work
}

EDIT 2: I learned that the Bioconductor packages are available in the bioconda channel so if you like me are using an environment.yaml file you can just list Bioconductor packages like so:

channels:
 - conda-forge
 - bioconda
 - defaults
dependencies:
 - r-base=4.1.1
 - r-ggplot2=3.3.5
 - bioconductor-ggbio

Solution

  • If you must install the package quietly you can do:

    suppressMessages({
    if (!requireNamespace("ggbio", quietly = TRUE))
        BiocManager::install("ggbio", quiet = TRUE)
    })
    

    But it's always a good idea to see if anything goes wrong with the installation.