0

I cannot figure out how to read from file based on command line argument:( All the answers I was able too google seemed too complicated.

I want to run this script from command line and let $1 be replaced by name of file I pass as an argument.

#!/usr/bin/Rscript
duplication<- read.table($1, header=T) 
options(scipen=10)
plot(duplication$x,duplication$y,col="blue");

So that by typing

R CMD BATCH script.R path_to_file

I want to read file and ideally output graph with the same name as was the name of file.

djot
  • 2,952
  • 4
  • 19
  • 28
Perlnika
  • 4,796
  • 8
  • 36
  • 47
  • As it turns out a very similar question was asked [yesterday](http://stackoverflow.com/questions/19345924/how-to-check-if-arguments-have-been-correctly-passed-to-rscript-on-windows) which may not only answer your question but give you some ideas. – Bryan Hanson Oct 14 '13 at 14:22
  • 1
    See: http://stackoverflow.com/questions/4808169/r-command-line-passing-a-filename-to-script-in-arguments-windows Note that the accepted answer works on other platforms too. – G. Grothendieck Oct 14 '13 at 14:24
  • You are right, I was trying R CMD BATCH and it turned out that Rscript worked on my mac. Thanks a lot. – Perlnika Oct 14 '13 at 14:28
  • I cannot accept your answer since it is comment. Should I delete my post then? – Perlnika Oct 14 '13 at 15:23

1 Answers1

0

Take a look at commandArgs. You should be able to do something like:

args <- commandArgs(trailingOnly = FALSE)

Then access the arguments as a vector. See also this answer

Community
  • 1
  • 1
Adam Hyland
  • 878
  • 1
  • 9
  • 21