我使用Rcaller
从R和Java之间的以下网站桥接连接获得了以下代码http://www.mhsatman.com/rcaller.php
在Windows上的NETBEANS IDE下运行它会显示以下警告:
Note:C:\Users\aman\Documents\NetBeansProjects\JavaApplicationRCaller\src\javaapplicationrcaller\JavaApplicationRCaller.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
但它也显示了这一点,而不是打印结果,即
rcaller.exception.RCallerExecutionException: Can not run C:\Program Files\R\R-
3.0.1\bin\i386\Rscript. Reason: java.io.IOException: Cannot run program
"C:\Program": CreateProcess error=2, The system cannot find the file specified
这是RScript可执行代码路径:
C:\ Program Files \ R \ R-3.0.1 \ bin \ i386 \ Rscript
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());
}
}
}
答案 0 :(得分:2)
这是最终答案: 我通过使用和安装以下内容来解决错误(我应该在这里为其他人提及):
install.packages("Runiversal",repos="cran.r-project.org")
然后:
install.packages("Runiversal")
答案 1 :(得分:1)
关于您的错误,这是由R可执行文件的路径中的空格引起的。您可以尝试转义空间(caller.setRscriptExecutable("C:\\Program\ Files\\R\\R-3.0.1\\bin\\i386\\Rscript");
(注意空格前的额外\
。)或者您可以简单地将R重新安装到不包含空格的路径(例如c:\\R
)。最后一个解决方案是最强大的。
答案 2 :(得分:0)
RCaller 2.2 不需要有问题的 Runiversal 包。访问博客文章了解详细信息here。