Installing R packages and running scripts from the command line (Seb)

Installing R packages from the command line (on the cluster and/or on your own computer)

Note that in this post, lines preceded by the dollar sign ($) mean commands typed directly from your shell session. Lines preceded by the greaterthan sign (>), mean commands typed from an R session.

1.How to install an R package. First, open R:

$ R

You have now opened an R session. Then type:

> install.packages(“qvalue”)

Select the mirror closest to you. The first time you do this, R will set up a directory automatically in “username/R/x86_64-redhat-linux-gnu-library/version” where all packages will be installed. Username will be your own username and version will be the version of R you are running (2.XX).

#Next time, you install a package, you will have to specify where your package directory is:

> install.packages(“qvalue”, lib = “/SciBorg/homes/username/R/x86_64-redhat-linux-gnu-library/version”)

2. 1.How to install a Bioconductor R package.

Many packages we use in R for high-throughput genomic data analysis actually come from Bioconductor. If your dream package comes from there, you need to tell R, prior to installing it.


>biocLite(“Biobase”, lib = “/SciBorg/homes/username/R/x86_64-redhat-linux-gnu-library/version”)

3. Once packages are installed, they can be loaded easily.

> library(qvalue)

Packages need to be loaded every time you open a new session, but installed only once.


Running R scripts from the command line.

Once you have written your script and everything works well, you probably do not want to open R and copy paste your script every time you run this analysis. You can create an executable shell script which will tells your computer to open R, run your script, output results and close R. Here is how:

1. Create an R script (a regular text file with your analysis) using your text editor of choice.

2. In the first line of the script, type: #!/usr/bin/Rscript –verbose. This will tell the computer to open R when it reads that line. Save it as a text file and close the file (say you call it script.R).

3. Back in your shell, change the permissions of the file to make it executable.

$ chmod +x script.R

4. You are now ready to run your whole analysis, without even having to open R or copy/paste anything. Run script by typing

$ ./script.R

Note: I usually have my R scripts outputting progress messages to tell me how far along I am in my analysis. Something like “At 13h44, you are at gene 666 of 1 million”. I like to save those progress messages by running my R scripts in the background and saving them in a log file. I do it like this:

$ nohup ./R.script >log.file &


You can also run scripts from the command line without having made them executable first:

$ R CMD example.r

Hope this helps, suggestions/ add-ons are welcomed.