0

I have simple java program which reads data from csv file and validate the data then call the R script to calculate the average of list of values, i'm using Renjin to call R program. I am able to call R script without passing parameter but here i need to pass list of values to my R script.

Here is my java code:

// listofval --list of float values example listofval= [3.3,5.2,6,9]

RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
engine.put("input",listofval);
engine.eval(new java.io.FileReader("avg.R"));

Here is my Rscript:

args=(commandArgs(TRUE))
args <- eval(parse(text=args[1]))
input <- args
customMean <- function(vector) {
mean(vector)
}
result.mean<- customMean(input)
print(result.mean)

I'm looking for work around, Can someone please help me on this?

Thanks in advance.

2 Answers 2

0

You can try passing arguments to your engine using sessions in java.

Please customize and/or try the following logic.

Session session = new SessionBuilder()
.withDefaultPackages()
.build();

session.setCommandLineArguments("/usr/bin/renjin", "X", "Y", "--args", "Z");

RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
RenjinScriptEngine engine = factory.getScriptEngine(session);

engine.eval("print(commandArgs(trailingOnly = FALSE))"); 
engine.eval("print(commandArgs(trailingOnly = TRUE))");  
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ahmet Eroğlu , thanks for your answer, I tried this approach Script is running but its not taking the argument.
0

I was able to resolve my issue, hope it could help someone! In R script we need to remove below line so that program is not expecting command line argument.

args=(commandArgs(TRUE))    
args <- eval(parse(text=args[1]))
input <- args

In java program i have saved my argument as input, in R environment it will be accessed as input

engine.put("input",listofval);

Final Rscript:

customMean <- function(vector) {
 return mean(vector)
}
result.mean<- customMean(input)

Comments

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.