0

我使用 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());
    }
}


     }
4

3 回答 3

2

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

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

接着:

install.packages("Runiversal") 
于 2013-06-21T06:57:26.863 回答
1

关于您的错误,这是由 R 可执行文件路径中的空格引起的。您可以尝试转义空格(caller.setRscriptExecutable("C:\\Program\ Files\\R\\R-3.0.1\\bin\\i386\\Rscript");(注意空格前的额外\内容)。或者您可以简单地将 R 重新安装到不包含空格的路径(例如c:\\R)。最后一个解决方案是最强大的。

于 2013-06-21T05:33:54.163 回答
0

RCaller 2.2不需要有问题的 Runiversal包。在此处访问博客条目以获取详细信息。

于 2014-04-13T09:01:40.573 回答