gpt4 book ai didi

java - R 和 Java 之间的桥梁错误

转载 作者:行者123 更新时间:2023-12-01 23:44:53 24 4
gpt4 key购买 nike

我从以下网站获得了以下代码,使用 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());
}
}


}

最佳答案

这是最终的答案: 我通过使用和安装以下内容解决了该错误(我应该在这里为其他人提及):

install.packages("Runiversal",repos="cran.r-project.org") 

然后:

install.packages("Runiversal") 

关于java - R 和 Java 之间的桥梁错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17228224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com