0

I use R in batch mode from command line to create several plots. As a first parameter I pass the input file name with the data sets. A second parameter holds a path to plot the result to. As a third parameter I want to pass the label name for the y-axis (ylab). Here comes my problem: The code below uses only the " and the first word within the third parameter as a y-axis label.

#! /usr/bin/env Rscript

library("igraph")
library("Rlab") # For xline command

args <- commandArgs(TRUE)

similarities <- scan(args[1])
similarities <- as.numeric(similarities)

# Open up a new pdf file for output given as cmd-line argument 2
pdf(args[2])

plot(similarities, xlab="Depth", type="l", col="red", ylab=args[3])
dev.off() 

q()

Calling this script with the following command:

Script.r input.txt plot.pdf "Some y-axis label"

Produces a plot as shown in the following figure

enter image description here

When I change the line for the plot command to:

plot(similarities, xlab="Depth", type="l", col="red", ylab="Some y-axis label")

everything works as expected.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
  • You could try and escape your spaces `Plot\ title`, and this recent question might also be interesting: http://stackoverflow.com/questions/13721872/r-making-commandargs-comma-delimited-or-parsing-spaces. – Paul Hiemstra Dec 05 '12 at 11:31
  • Debug: check to see what the contents of `args` is, to make sure you are (or are not) reading your command-line text into a single element. – Carl Witthoft Dec 05 '12 at 12:18
  • Ok- it's been a Java problem - I had a syscall from Java and it seems like Java is somehow escaping the " " characters. It worked from the command line, so I replace the space with __ and re-replaced it in R to avoid the " " - now everything works. Thanks a lot! – Michael Lihs Dec 07 '12 at 13:59

0 Answers0