简体   繁体   中英

How To Use Rcaller with Java Servlet and read CSV file

I'm using R programing to analysis FFT . now I want to make Java web application/ java servlet and calling R to use Rcaller/Rcode for it . I have some reference about Calling Rcode in java application. http://code.google.com/p/rcaller/wiki/Examples I have CSV File for example A.csv
time Amplitude
1 0.00000 -0.021
2 0.00001 -0.024
3 0.00003 -0.013
4 0.00004 -0.023
5 0.00005 0.019
6 0.00007 -0.002
7 0.00008 -0.013
then I want to upload this file and use R Code for analysis FFT and Plot it. Help is much appreciated! Thanks in advance, Maria

You start creating an instance of RCaller and set the current location of install Rscript.exe file. You can start with

RCaller caller = new RCaller();
Globals.detect_current_rscript();
caller.setRscriptExecutable(Globals.Rscript_current);
RCode code = new RCode();

or you can give the exact location

RCaller caller = new RCaller();
caller.setRscriptExecutable("c:\\path\\to\\Rscript.exe");
RCode code = new RCode();

Suppose your data is saved in a file mydata.csv.

code.addRCode("dat <- read.cvs(\"mydata.csv\", header=T, sep=\",\"");

then we are plotting the Amplitude

File file = code.startPlot();
code.addRCode("plot.ts(dat$Amplitude)");
code.endPlot();

and sending our code to R:

caller.setRCode(code);
caller.runOnly();

And now, the file variable holds the image data. It can be shown on screen using the code

code.showPlot(file);

For further reading, follow the blog entries on stdioe blog

When I execute this code is running but didn't show anything !!!!!!!

package test2;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.swing.ImageIcon;
import rcaller.RCaller;
import rcaller.RCode;
import rcaller.exception.RCallerExecutionException;
import rcaller.exception.RCallerParseException;

public class Test2 {
public static void main(String[] args) {
Test2 test2=new Test2();

}
 private int span;
 @SuppressWarnings("empty-statement") 


 public void test2()throws IOException{
  try {

  RCaller caller = new RCaller();
  caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.0.3\\bin\\Rscript.exe");
  RCode code = new RCode();

  code.addRCode("dat<-read.csv(\"NetBeansProjects\"test2\"A.csv\",header=T,sep=\",\"");

  File file=code.startPlot();
  code.addRCode("plot.ts(dat$Amplitude)");
  code.endPlot();

  caller.setRCode(code);
  caller.runOnly();
  ImageIcon i=code.getPlot(file);
  code.showPlot(file);

} catch (RCallerExecutionException | RCallerParseException e) {
  System.out.println(e.toString());
}

 } 

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM