Biostar Beta. Not for public use.
Automation of R package loading
1
Entering edit mode
3.0 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 • 1.2k views
ADD COMMENTlink
2
Entering edit mode

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

ADD REPLYlink
0
Entering edit mode

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

ADD REPLYlink
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 REPLYlink
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 REPLYlink
2
Entering edit mode
14 months ago
National Institutes of Health, Bethesda…

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 COMMENTlink
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 REPLYlink
1
Entering edit mode
18 months ago
United States

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 COMMENTlink
0
Entering edit mode

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

ADD REPLYlink

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.1