How to run R code using JAVA program?

0 votes

I am a new bee in R. I am basically trying to run an R script by using a Java class. 

 I implemented Rserve for this but, the execution stopped after creating an instance of "RConnection". Similarly, I used rJava but i got an exception stating: "java.lang.UnsatisfiedLinkError: jri.dll: Can't find dependent libraries".

This is my code:

For rJava:

    import org.rosuda.JRI.Rengine;
public class Temp {

public static void main(String a[]) {
    // Create an R vector in the form of a string.
    String javaVector = "c(1,2,3,4,5)";

    // Start Rengine.
    Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);

    // The vector that was created in JAVA context is stored in 'rVector' which is a variable in R context.
    engine.eval("rVector=" + javaVector);

    //Calculate MEAN of vector using R syntax.
    engine.eval("meanVal=mean(rVector)");

    //Retrieve MEAN value
    double mean = engine.eval("meanVal").asDouble();

    //Print output values
    System.out.println("Mean of given vector is=" + mean);

}
}

For Rserve:

import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class Temp {

public static void main(String a[]) {
    RConnection connection = null;
    System.out.println("line 10");
    try {
        // Create a connection to Rserve instance running on default port 6311

        System.out.println("line 15");
        connection = new RConnection();
        System.out.println("line 17");
        //Note four slashes (\\\\) in the path
        connection.eval("source('D:\\\\RExamples\\\\helloworld.R')");
        System.out.println("line 19");
        int num1=10;
        int num2=20;
        int sum=connection.eval("myAdd("+num1+","+num2+")").asInteger();
        System.out.println("The sum is=" + sum);
    } catch (RserveException e) {
        e.printStackTrace();
    } catch (REXPMismatchException e) {
        e.printStackTrace();
    }
}
}

Any help would be appreciated.

Mar 11 in Data Analytics by Tyrion anex
• 8,280 points
57 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You're trying to call an external application. This should work:

Runtime.getRuntime().exec("Rscript myScript.R"); 
answered Mar 11 by Sophie may
• 9,090 points

Related Questions In Data Analytics

0 votes
1 answer

How to change y axis max in time series using R?

The axis limits are being set using ...READ MORE

answered Apr 3, 2018 in Data Analytics by darklord
• 6,140 points
42 views
0 votes
1 answer
0 votes
1 answer

How to use group by for multiple columns in dplyr, using string vector input in R?

dplyr added versions for group_by. This allows you ...READ MORE

answered Apr 12, 2018 in Data Analytics by CodingByHeart77
• 3,680 points

edited Apr 12, 2018 by CodingByHeart77 1,680 views
0 votes
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
61 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
29 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
39 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
65 views
0 votes
1 answer

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
30 views
0 votes
1 answer
0 votes
1 answer

Using CMD Windows to execute R language

So, Command line arguments can't be directly ...READ MORE

answered Jan 14 in Data Analytics by Sophie may
• 9,090 points
44 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.