Using R in a task type¶
R can be used in a task type if Core.Host.RScript
is installed.
For example Example.RScript.PlotXY
uses R to create a simple plot:
The main script.py
contains the boilerplate XamFlow input and output handling,
and uses runutil.run()
to execute an R script:
import xfworkerutil
import runutil
worker = xfworkerutil.XFWorkerJob()
inputs = worker.job['inputs']
# Download input files
table_path = worker.download_input_file(inputs['table'])
plot_path = worker.create_related_path(table_path, '_plot.png')
# Run R script
runutil.run_rscript(["Reg_1x_1y.R", str(table_path), str(plot_path)])
# Upload output files
worker.upload_output_file(plot_path)
# Save outputs
outputs = {
"plot": worker.create_output_bitmap(plot_path.name),
}
worker.finish(outputs)
The Reg_1x_1y.R
script contains the R commands:
library(ggplot2)
args = commandArgs(trailingOnly=TRUE)
# Open CSV-file
df = read.csv(args[1], header=TRUE)
# Column nr x-axis
x = colnames(df)[1]
# Column nr y-axis
y = colnames(df)[2]
# Regression line
myplot = ggplot(df) +
geom_jitter(aes_string(x, y), shape=1, colour="blue") +
geom_smooth(aes_string(x, y), method=lm, se=TRUE, color="blue")
# Save plot
ggsave(myplot, file=args[2])