0

I have a R script in which I want to call parameters from Java code. The parameters are csv file name file name and unique ID which has to be used to name the two output files.

My R script is :

 df1 <- read.csv("filename.csv")
 vs=colnames(df1)
 md=formula(paste(vs[3],"~",vs[1],"+",vs[2]))
 fit <- summary(aov(md, data=df1))[[1]]

 #text output   
 names(fit)[1:4]=c("DF","SS","MS","F")
 sink("test.txt")

In this code the first line df1 <- read.csv("filename.csv") should take file name dynamically from JAVA code and the last line sink("test.txt") should take unique ID and create the output file.

The java code is :

buildCommand.add("Rscript  ");  
buildCommand.add(scriptName);
buildCommand.add(inputFileWithPathExtension);
buildCommand.add(uniqueIdForR); 

I have seen other post but I am unsure wether it will help in my case, also similar posts talking about rJava package`, but didn't get clear idea.

Any help will be highly appreciated. thanks in advance !

K.S
  • 113
  • 13
  • Possible duplicate of [How can I read command line parameters from an R script?](https://stackoverflow.com/questions/2151212/how-can-i-read-command-line-parameters-from-an-r-script) – Ralf Stubner Aug 06 '18 at 09:01
  • Once you make sure that the final system command looks like `Rscript scriptName inputFile uniqueId`, you only have to handle the command line parameters in R, which is not Java specific. – Ralf Stubner Aug 06 '18 at 09:04
  • @RalfStubner Im new to this, so should it be something like this `args <- commandArgs(TRUE)` `input <- read.csv(args[1])`. – K.S Aug 06 '18 at 09:48
  • Yes, that looks correct. – Ralf Stubner Aug 06 '18 at 09:53
  • @RalfStubner How should I use the unique ID that is passed as argument in java code to name the output file in R. Should it be added as another argument like `unique_Id <- as.integer(args[2])` – K.S Aug 06 '18 at 09:59

1 Answers1

1

Here a very simple example for reading command line arguments in your case:

args <- commandArgs(TRUE)
input <- args[1]
output <- paste0(args[2], ".txt")

cat("Reading from", input, "\n")
cat("Writing to", output, "\n")

Example:

$ Rscript foo.R foo.csv 1234567
Reading from foo.csv 
Writing to 1234567.txt 
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Thank you so much for the answer. – K.S Aug 06 '18 at 10:13
  • Im getting an error : `Error in parse(text = x, keep.source = FALSE) : :2:0: unexpected end of input 1: ~ + ^` in the line `vs=colnames(df1) md=formula(paste(vs[3],"~",vs[1],"+",vs[2]))` – K.S Aug 06 '18 at 15:02
  • @K.S This looks as if the `vs[i]` are empty. You probably should add a test like `any(nchar(vs[1:3]) == 0)`. If that is `TRUE` you should stop execution and output `vs`. – Ralf Stubner Aug 06 '18 at 15:45
  • This error was not there earlier when the input file was hard coded. Is it because of this line `df1 <- input`. – K.S Aug 06 '18 at 16:20
  • That line should probably read `df1 <- read.csv(input)`. And in the end you should have `sink(output)`. – Ralf Stubner Aug 06 '18 at 16:22
  • Will try the following. Also for output I have .svg as image, can i add it as `output <- paste0(args[2], ".txt", ''.svg")` – K.S Aug 06 '18 at 16:25
  • @K.S You probably want `output1 <- paste0(args[2], ".txt")` and `output2 <- paste0(args[2], ".svg")`. But I am not 100% sure what your aim is. – Ralf Stubner Aug 06 '18 at 16:51
  • My aim is to pass `input file` dynamically to R script and name the output files `.txt` and `.svg` as unique ID. Both `input file` and `unique ID` are arguments from `Java` code. Further the R script is for various statistical analysis. – K.S Aug 06 '18 at 16:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177509/discussion-between-ralf-stubner-and-k-s). – Ralf Stubner Aug 06 '18 at 16:56