# Writing R Modules

If you are writing R code to be invoked as a GenePattern module, follow the guidelines in Writing Modules for GenePattern. This section provides additional information for modules written in R:

## Adapting R Code

When you create a module in GenePattern, you specify the command line that invokes the program that performs the desired function. Generally, the command line includes arguments, such as the parameters for the algorithm and the data file to analyze. For example, the following command line invokes the myfunc() function in the R script named myscript.R, passing a single parameter, input.filename:

  myscript.R myfunc

Calling R script from a command line is possible, but generally not useful because you cannot pass arguments to the script. To pass arguments to your R code, create a function. For example:

 myfunc <- function ( input.filename ) ... (your R-code here)

## Using Parameter Flags

When designing the program and its command line, use parameter flags (for example, -f input_file) rather than relying on parameter positions. Parameter flags allow users to build command lines with variable numbers of arguments, which makes it easy to omit optional parameters. When writing R code, if you have optional input parameters on the command line, you must use named rather than positional parameters in the command line definition.

For example, to write an R function that takes a filename as input, the main function might be:

 myfunction <- function(...) { args <- list(...) for(i in 1:length(args)) { flag <- substring(args[[i]], 0, 2) value <- substring(args[[i]], 3, nchar(args[[i]])) if(flag=='-i') { ... } 

The command line for the module would be:

  myscript.R myfunction -i

## Using an R Library in a GenePattern Module

In order to facilitate the use of R libraries in GenePattern modules, the GenePattern team maintains a list of R libraries and makes them available in the module repository. To make use of these libraries, you need only make an edit (to be described below) to the module manifest, which causes the necessary libraries to be downloaded and installed, if the library does not already exist on that server. Once installed, the R libraries will be available to any other module which requires the same library.

This section describes how to do this using the RLibraryDemo module as an example.

### What Are the Benefits of This Method?

With this method of installing R libraries there is no need to include all of the libraries with the module package; nor do you write the extra code to install those libraries when the module is first run. This also means that, should any problems with the libraries arise during the installation of the module, an error will be thrown at that time, rather than seeming to install correctly and failing at run time. Lastly, but no less importantly, this allows you to install the libraries only one time per server and to share those libraries between modules, rather than installing for each individual module which needs them.

### How to Include an R Library

The easiest way is to use one the GenePattern team has already provided and/or use our demo module, RLibraryDemo, as a template for your own.

For a list of available R libraries from the GenePattern team, please contact us at gp-help AT broadinstitute DOT org. The current list is available at ftp://ftp.broadinstitute.org/pub/genepattern/plugins/rlib/ in folders organized by R version.

The RLibraryDemo module is available here. Once you have downloaded the module, open the zip file and take a look at the module's "manifest".

Notice the "requiredPatchLSIDs" and "requiredPatchURLs" parameters. For each library required by your module, you must specify both the LSID of the library, as well as the URL to the location where the library can be accessed.

Note that in order to use this example module, you will need to install R 2.10. For information on how to do so please see Using Different Versions of R in GenePattern.

Updated on September 03, 2012 12:34