Automation of R package loading
2
1
Entering edit mode
8.2 years ago

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 • 2.5k views
ADD COMMENT
2
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
2
Entering edit mode
8.2 years ago

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
8.2 years ago

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.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2627 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6