This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Automation of R package loading

Hello All,

So I am trying to completely automate my pipeline. I am to have a bunch of R scripts in the background and then just one script a user can interact with that has a bunch of functions. Naturally I want the package loading to be a part of this. I tried to create the following function but it does not work. It will try to run the code in the function automatically without making it. The idea is if the user selects 1 it will install and load the packages, otherwise just load them. Any ideas?

loadpackages <- function(x)
  {  
  if (x = 1) {
    source("http://bioconductor.org/biocLite.R")
    biocLite("oligo")
    biocLite("limma")
    biocLite("pd.hugene.2.1.st")
    biocLite("hugene20sttranscriptcluster.db")
    biocLite("genefilter")
    biocLite("annotate")
    library(limma)
    library(oligo)
    librarypd.hugene.2.0.st)
    library(genefilter)
    library(annotate)
    library(hugene20sttranscriptcluster.db)
    }
  else {
    library(limma)
    library(oligo)
    librarypd.hugene.2.0.st)
    library(genefilter)
    library(annotate)
    library(hugene20sttranscriptcluster.db)
    }
  }
r bioconductor

isn't it x == 1 for a comparative operator?

Opps...well that was embarrassing. I did not notice that.

Why not just have a directory containing the libraries? We do that for some of our pipelines so people don't have to bother installing all of the packages themselves.

I tried doing that using packrat and it did not work for me. There were a few errors. I suppose I can do it manually?

2 answers

Doing package installation in a script is a fairly brittle solution. Instead, package your scripts as an R package and then use the normal package dependency mechanisms deal with the issues. Building an R package is a very straightforward process. Then, you can do:

install.packages('mypipelinepkg',repos=...)

All necessary dependencies are installed and all code is readily available.

Thank you so much for this response! My pipeline is now a fully functioning R package! This was a lot of help.

I think you want the function require. It's like library, but returns TRUE/FALSE if it could load, instead of an error.

Then you say

if(!require(p)) {
    biocLite(p);library(p)
}

and it will either load, or download if it needs to.

When I try this in my session it automatically starts downloading even though I already have packages downloaded.

Log in to answer this question.