# Introduction

This page describes how to install the eclipse plugin that helps calling R from Java
using JRI. This tutorial describes the Java Wrappers for R objects. Lets now look at the following

1. Calling R functions from java
2. Assigning values to R objects from Java
3. Retrieving values from R into Java

# Calling R functions from java

The org.rosuda.JRI.Rengine class has methods that allow calling R functions from Java. There are two ways to execute functions. Either call
each function using the Rengine, or open up a console to R.We would normally want to do the former. There are two kinds of API calls from Java to R.
High level and low level. It is preferable to use the high level calls when possible, however if low level calls have to be made then make sure to
apply synchronization to the method calls. Also only one instance of Rengine can be created, since the API does not allow creating multiple Rengine
instances from different threads. (no parallel programming support)

# eval(String s,boolean convert)

This is probably the most important method of this API. ‘s’ is the expression that will be parsed and evaluated. The boolean ‘convert’
specifies whether the resulting REXP is empty(false) or contains the native implementation of the result. The example below creates a variable x,
finds and prints its mean

```re.eval("x=c(1:10)");
double d = re.eval("mean(x)").asDouble();
System.out.println(d);
// prints 5.5
```

# Assigning Value to an R variable.

The eval function would not be very helpful if there was no way to pass values values to the R functions. For example, lets say you want to use the
power of R to calculate the mean of 10 numbers. Ok, you dont look convinced, i should probably get a better example. How about fitting points to a
linear equation y = mx+c. y and x are arrays passed to R. ‘eval’ is used to fit y and x to y=mx +c and m and c are brought back to R. Here’s
how we do it. We use the re.assign(“variable”,value) to assign a value to the R variable.

```// Assign y to similarly named variable in R
re.assign("y", y);
// Lets see if it was assigned correctly
System.out.println(Arrays.toString(re.eval("y").asDoubleArray()));
// output [11.1, 13.2, 15.6, 27.4, 39.2, 113.1, 135.1]
// Assign x to similarly named variable in R
re.assign("x", x);
// fit to y=mx+c
re.eval("a=lm(y~x)");
// lets print the values
RVector fit = re.eval("a").asVector();
System.out.println("The intercept is " + fit.at(0).asDoubleArray());
// prints The intercept is -19.60087719298246
System.out.println("The slope is " + fit.at(0).asDoubleArray());
// prints The slope is 8.481140350877192
```

### 3 thoughts on “Execute R from Java”

1. Hi
very thanks to helpful text.Do I need to define that variable within the R console before assigning the value to the variable within R?
thanks.

2. Hi, thanks, a very useful plugin & tutorial.
Everything set OK at first attempt so I’ve been able to play a bit with R in my java application since yesterday and everything worked fine. But now I’ve got a doubt which perhaps you could solve:

¿How do I export the R run configuration within an executable jar file so I can run my Java application with R code outside Eclipse? I’ve found that the export window for java runnables don’t let you select the R launch config, although you can export it separately.

3.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8246008,"math_prob":0.87546045,"size":3515,"snap":"2021-43-2021-49","text_gpt3_token_len":857,"char_repetition_ratio":0.09940188,"word_repetition_ratio":0.010362694,"special_character_ratio":0.25405404,"punctuation_ratio":0.14171124,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9885971,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T14:06:56Z\",\"WARC-Record-ID\":\"<urn:uuid:a762098e-fe58-492e-bd49-0775c9c238cb>\",\"Content-Length\":\"56255\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4bd91e17-6fff-40da-a321-432aa0fb8848>\",\"WARC-Concurrent-To\":\"<urn:uuid:828d982d-6f10-48e2-b1cf-6352f60afc3d>\",\"WARC-IP-Address\":\"50.112.213.28\",\"WARC-Target-URI\":\"https://www.studytrails.com/2016/09/14/execute-r-from-java/\",\"WARC-Payload-Digest\":\"sha1:WS6X5NK74JXQDLRQ4L2QH5D4AOQJF6AH\",\"WARC-Block-Digest\":\"sha1:AYKRNNNKWLEECS3FA3YTM277RORITR7P\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587908.20_warc_CC-MAIN-20211026134839-20211026164839-00226.warc.gz\"}"}