Creating a Galaxy tool for R scripts that output images and PDFs

This is code snippets for creating galaxy tool wrappers for R scripts that produce one or more images as output. For now, just the code. Description (hopefully) to be added later).

Different outputs as separate jobs

ToolConfig / XML Wrapper file:

<?xml version="."?>
<tool id="rtest" name="R Test" version="." hidden="false">
<description>Test executing a simple R script and return some produced image</description>
 
<command interpreter="Rscript">
    rtest.r --pdffile=$pdffile --pngfile=$pngfile
</command>
 
<inputs>
</inputs>
 
<outputs>
  <data format="png" name="pngfile"/>
  <data format="pdf" name="pdffile"/>
</outputs>
 
<help>
**What it does**
 
This tool executes a test R script.
</help>
</tool>

The R code:

#!/usr/bin/Rscript
# Import some required libraries
library('getopt');
 
# Make an option specification, with the following arguments:
# 1. long flag
# 2. short flag
# 3. argument type: 0: No argument, 1: required, 2: optional
# 4. target data type
option_specification = matrix(c(
  'pdffile', 'f', 2, 'character',
  'pngfile', 'g', 2, 'character'
), byrow=TRUE, ncol=4);
 
# Parse options
options = getopt(option_specification);
 
# Create some simple test data
x = seq(0,10,1);
y = x * 10;
 
# Produce PDF file
if (!is.null(options$pdffile)) {
  pdf(options$pdffile);
  plot(x, y);
  dev.off();
}
 
# Produce PNG file
if (!is.null(options$pngfile)) {
  png(options$pngfile);
  plot(x, y);
  dev.off();
}

All output in one job

ToolConfig / XML Wrapper file:

<?xml version="1.0"?>
<tool id="rtest2" name="R Test 2" version="1.0" hidden="false">
<description>Test executing a simple R script and return some produced image</description>
 
<command interpreter="Rscript">
    rtest2.r --outdir="$htmlfile.files_path" --htmlfile=$htmlfile
</command>
 
<inputs>
  <param name="job_name" type="text" label="Supply a name for the outputs to remind you what they contain" value="RTest"/>
</inputs>
 
<outputs>
  <data format="html" name="htmlfile" label="${job_name}.html" />
</outputs>
 
<help>
**What it does**
 
This tool executes a test R script, and produces HTML output with a
clickable image, that leads to a download of the PDF file.
</help>
</tool>

The R code:

#!/usr/bin/Rscript
# Import some required libraries
library('getopt');
 
# Make an option specification, with the following arguments:
# 1. long flag
# 2. short flag
# 3. argument type: 0: No argument, 1: required, 2: optional
# 4. target data type
option_specification = matrix(c(
  'outdir', 'f', 2, 'character',
  'htmlfile', 'h', 2, 'character'
), byrow=TRUE, ncol=4);
 
# Parse options
options = getopt(option_specification);
 
# Create some simple test data
x = seq(0,10,1);
y = x * 10;
 
if (!is.null(options$outdir)) {
  # Create the directory
  dir.create(options$outdir,FALSE)
}
 
# Strip whitespace
pdffile <- gsub("[ ]+", "", paste(options$outdir,"/pdffile.pdf"))
pngfile <- gsub("[ ]+", "", paste(options$outdir,"/pngfile.png"))
htmlfile <- gsub("[ ]+", "", paste(options$htmlfile))
 
# Produce PDF file
pdf(pdffile);
plot(x, y);
dev.off();
 
# Produce PNG file
png(pngfile);
plot(x, y);
dev.off();
 
# Produce the HTML file
htmlfile_handle <- file(htmlfile)
html_output = c('<html><body>',
                '<a href="pdffile.pdf"><img src="pngfile.png"/></a>',
                '</html></body>');
writeLines(html_output, htmlfile_handle);
close(htmlfile_handle);