Biostar Beta. Not for public use.
Question: Automation of R package loading
1
Entering edit mode

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)
    }
  }
ADD COMMENTlink 4.0 years ago hakimelakhrass • 70 • updated 17 months ago RamRS 21k
Entering edit mode
2

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

ADD REPLYlink 4.0 years ago
andrew.j.skelton73
5.7k
• updated 17 months ago
RamRS
21k
Entering edit mode
0

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

ADD REPLYlink 4.0 years ago
hakimelakhrass
• 70
Entering edit mode
1

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 4.0 years ago
Devon Ryan
90k
Entering edit mode
0

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 4.0 years ago
hakimelakhrass
• 70
2
Entering edit mode

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 4.0 years ago Sean Davis 25k • updated 17 months ago RamRS 21k
Entering edit mode
0

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

ADD REPLYlink 4.0 years ago
hakimelakhrass
• 70
1
Entering edit mode

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 4.0 years ago karl.stamm 3.5k • updated 17 months ago RamRS 21k
Entering edit mode
0

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

ADD REPLYlink 4.0 years ago
hakimelakhrass
• 70

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.0