简体   繁体   中英

Error under bridge between R and Java

I got the below code from the following website bridge connection between R and Java using Rcaller

http://www.mhsatman.com/rcaller.php

Running it under NETBEANS IDE on Windows shows the following warning:

              Note:C:\Users\aman\Documents\NetBeansProjects\JavaApplicationRCaller\src\javaapplicationrcaller\JavaApplicationRCaller.java uses or overrides a deprecated API.  
              Note: Recompile with -Xlint:deprecation for details.  

But it also shows this and not printing the results ie

    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

This is the RScript executable code path:
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());
    }
}


     }

This is the final answer: I solved the error by using and installing the following (I should mention it here for others):

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

and then:

install.packages("Runiversal") 

In regard to your error, this is caused by a space in the path to the R executable. You could try escaping the space ( caller.setRscriptExecutable("C:\\\\Program\\ Files\\\\R\\\\R-3.0.1\\\\bin\\\\i386\\\\Rscript"); (note the extra \\ before the space). Or you could simply reinstall R to a path that does not include a space (eg c:\\\\R ). This last solution is the most robust.

RCaller 2.2 does not require problematic Runiversal package. Visit the blog entry for details here .

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