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 !

5
  • Possible duplicate of How can I read command line parameters from an R script? Commented Aug 6, 2018 at 9: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. Commented Aug 6, 2018 at 9:04
  • @RalfStubner Im new to this, so should it be something like this args <- commandArgs(TRUE) input <- read.csv(args[1]). Commented Aug 6, 2018 at 9:48
  • Yes, that looks correct. Commented Aug 6, 2018 at 9: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]) Commented Aug 6, 2018 at 9:59

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you so much for the answer.
Im getting an error : Error in parse(text = x, keep.source = FALSE) : <text>:2:0: unexpected end of input 1: ~ + ^ in the line vs=colnames(df1) md=formula(paste(vs[3],"~",vs[1],"+",vs[2]))
@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.
This error was not there earlier when the input file was hard coded. Is it because of this line df1 <- input.
That line should probably read df1 <- read.csv(input). And in the end you should have sink(output).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.