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 12, 2019 in Data Analytics by Tyrion anex
• 8,700 points
5,109 views

1 answer to this question.

0 votes

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

Runtime.getRuntime().exec("Rscript myScript.R"); 
answered Mar 12, 2019 by Sophie may
• 10,610 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 Sahiti
• 6,370 points
3,528 views
0 votes
1 answer
0 votes
2 answers

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

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 6, 2019 in Data Analytics by anonymous
13,669 views
+1 vote
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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
6,324 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,660 points
832 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,660 points
5,541 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

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

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
854 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,660 points
1,190 views
0 votes
1 answer

Is there a way to terminate an R program without causing an error?

You can make use of the 'browser' ...READ MORE

answered Jan 10, 2019 in Data Analytics by Sophie may
• 10,610 points
706 views
0 votes
1 answer

Using CMD Windows to execute R language

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

answered Jan 14, 2019 in Data Analytics by Sophie may
• 10,610 points
860 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP