我从下面的网站得到了下面的代码使用Rcaller在R和Java之间的桥接连接
http://www.mhsatman.com/rcaller.php在Windows的NETBEANS IDE下运行它会显示以下警告:
Note:C:UsersamanDocumentsNetBeansProjectsJavaApplicationRCallersrcjavaapplicationrcallerJavaApplicationRCaller.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
但它也显示了这个,而不打印结果,即
rcaller.exception.RCallerExecutionException: Can not run C:Program FilesRR-
3.0.1bini386Rscript. Reason: java.io.IOException: Cannot run program
"C:Program": CreateProcess error=2, The system cannot find the file specified
这是RScript可执行代码路径:
C:Program FilesRR-3.0.1bini386Rscript
package javaapplicationexample;
import rcaller.RCaller;
import java.util.Random;
public class JavaApplicationExample {
public static void main(String[] args) {
new JavaApplicationExample();
}
public JavaApplicationExample(){
try{
/*
* Creating Java's random number generator
*/
Random random = new Random();
/*
* Creating RCaller
*/
RCaller caller = new RCaller();
/*
* Full path of the Rscript. Rscript is an executable file shipped with R.
* It is something like C:\Program File\R\bin.... in Windows
*/
// It is showing the same error when writing Rscript.exe here
caller.setRscriptExecutable("C:\Program Files\R\R-3.0.1\bin\i386\Rscript");
/* We are creating a random data from a normal distribution
* with zero mean and unit variance with size of 100
*/
double[] data = new double[100];
for (int i=0;i<data.length;i++){
data[i] = random.nextGaussian();
}
/*
* We are transferring the double array to R
*/
caller.addDoubleArray("x", data);
/*
* Adding R Code
*/
caller.addRCode("my.mean<-mean(x)");
caller.addRCode("my.var<-var(x)");
caller.addRCode("my.sd<-sd(x)");
caller.addRCode("my.min<-min(x)");
caller.addRCode("my.max<-max(x)");
caller.addRCode("my.standardized<-scale(x)");
/*
* Combining all of them in a single list() object
*/
caller.addRCode("my.all<-list(mean=my.mean, variance=my.var, sd=my.sd, min=my.min, max=my.max, std=my.standardized)");
/*
* We want to handle the list 'my.all'
*/
caller.runAndReturnResult("my.all");
double[] results;
/*
* Retrieving the 'mean' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("mean");
System.out.println("Mean is "+results[0]);
/*
* Retrieving the 'variance' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("variance");
System.out.println("Variance is "+results[0]);
/*
* Retrieving the 'sd' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("sd");
System.out.println("Standard deviation is "+results[0]);
/*
* Retrieving the 'min' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("min");
System.out.println("Minimum is "+results[0]);
/*
* Retrieving the 'max' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("max");
System.out.println("Maximum is "+results[0]);
/*
* Retrieving the 'std' element of list 'my.all'
*/
results = caller.getParser().getAsDoubleArray("std");
/*
* Now we are retrieving the standardized form of vector x
*/
System.out.println("Standardized x is ");
for (int i=0;i<results.length;i++) System.out.print(results[i]+", ");
}catch(Exception e){
System.out.println(e.toString());
}
}
}
这是最终答案:我通过使用和安装以下程序解决了这个错误(我应该在这里为其他人提到它):
install.packages("Runiversal",repos="cran.r-project.org")
然后:
install.packages("Runiversal")
关于您的错误,这是由R可执行文件路径中的空格引起的。您可以尝试转义空格(caller.setRscriptExecutable("C:\Program Files\R\R-3.0.1\bin\i386\Rscript");
(注意空格前额外的))。或者您可以简单地将R重新安装到不包含空格的路径(例如
c:\R
)。最后一个解决方案是最健壮的。
RCaller 2.2不需要有问题的Runiversal包。在这里访问博客条目了解详细信息。