-1

I want to generate a R boxplot by using rcaller with java. My code is :

try {
        RCaller caller = new RCaller();
        caller.setRExecutable("/usr/bin/R");
        caller.setGraphicsTheme(new DefaultTheme());

        RCode code = new RCode();
        code.clear();

        File file = code.startPlot();

        code.addRCode("boxplot((1:10),main=\"1-10\")");

        System.out.println(code.toString());
        code.endPlot();

        caller.setRCode(code);
        caller.runAndReturnResultOnline("boxplot(1:10),main=\"1-10\"");
        code.showPlot(file);

But it does not keep run on codecaller.runAndReturnResultOnline("boxplot(1:10),main=\"1-10\"");

i try to use code below ,that can plot a R plot.what differences between them ?

try {
        RCaller caller = new RCaller();
        caller.setRExecutable("/usr/bin/R");
        caller.setGraphicsTheme(new DefaultTheme());

        RCode code = new RCode();
        code.clear();

        double[] numbers = new double[] { 1, 4, 4, 5, 6, 10 };
        code.addDoubleArray("x", numbers);

        File file = code.startPlot();
        System.out.println(file.toString());

        code.addRCode("plot.ts(x)");
        System.out.println(code.toString());
        code.endPlot();

        caller.setRCode(code);
        caller.runAndReturnResultOnline("plot.ts(x)");
        code.showPlot(file);
jbytecode
  • 681
  • 12
  • 29

1 Answers1

0

Replace your line :

code.addRCode("boxplot((1:10),main=\"1-10\")");

By this :

code.addRCode("boxplot(c(1:10),main='1-10')");

Or as in the second example (the working one), you can give your x vector from java and replace this line:

caller.runAndReturnResultOnline("plot.ts(x)");

by

caller.runAndReturnResultOnline("boxplot(x)");

PS: I dont' have java to test.

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thanks for you answering!I tried your method ,it works!Thanks!But there is another question:if my code is :`code.addRCode("bp<-boxplot((data$" + "pp" + "),xlab=\"" + "pp"+ "\",main=\"" + "pp" + " boxPlots\")");` ...The data is a data.frame.It does not work.Could you please help me...Thank you very much! – Yolanda Wang Jun 21 '13 at 13:06
  • You should do something like `code.addRCode("paste0("bp<-boxplot((data$" , "pp" , "),xlab='" , "pp", "',main='" , "pp" , " boxPlots')")` But this suppose that you create your data.frame before : `code.addRCode("data <- data.frame(pp=1:10)")` – agstudy Jun 21 '13 at 13:11
  • @YolandaWang You try to mix java and R code. Maybe you know some java, but I advise you seriously to get some R tutorial and get the R basics before trying to call it from another language. – agstudy Jun 21 '13 at 14:51
  • i use paste,it works.not paste0...I looked for some detail about paste.It benefits me a lot!Thank you! – Yolanda Wang Jun 23 '13 at 03:06
  • @YolandaWang `paste0(..., collapse) is equivalent to paste(..., sep = "", collapse), slightly more efficiently.` from the help of paste...: I don't see why it would work with paste and not with paste0... – agstudy Jun 23 '13 at 11:04