This post shows how to call user-defined functions in R script from Eclipse Java with rJava package. This work will improve code readability and minimize the likelihood of errors in such a way that it reduces multiples lines of R codes.
rJava with user-defined R functions
We’ve learned how to insert R commands to Eclipse Java in the previous post. But as the number of lines of R commands is too many, overall code readability and maintenance could be deteriorated. In this case, it is preferred to use R script which contains user-defined or built-in R functions. This means that essencially one line of code will do rather than multiple lines of commands.
Suppose that we performs Lasso regression in Java using R script.
For detailed information regarding the first environment setting and Lasso model, refer to the following post.
Before going into the details, overall setting for rJava in Eclipse is required, which is discussed in the previous post.
Functionsin R Script to be Called
R scripts for Lasso estimation is written as a separate file. In particular, glmnet package is used. Like this, when using another package library, one more rJava command is also needed, which is discussed later.
With source() command, test_glmnet() function in shlee_RLib.R can be called in another R script as follows.
rm(list = ls()) # remove all files from your workspace
test_glmnet(10) command returns the folloiwng results, which are coefficient vectors of two Lasso models (\(\lambda = 0.01, 0.05\)).
Calling User-defined Functions in Eclipse Java
At first, let’s make an Eclipse java class file in which R functions are called using rJava. We name it CRJava2.class for example. In CRjava2.class file, write the folloiwng java code.
In fact, it is interesting that the essencial part of the above R jave code is calling test_glmnet(). We can save many lines of code, which depends on the extent or size of calcuations or estimations.
REXP x = re.eval(“test_glmnet(“+nvar+“)”);
Point to Note
There is one important thing to know. When other (not built-in) libraries such as glmnet are included, the following rJava command is necessary. It is important.
Without the above rJava command, Eclipse returns an error message with “there is no package called ‘glmnet'”.
First Run and Errors
When we run the above Java code, we encounter the following errors. Hence, we need to do some settings for CRjava2.class file.
But this first running this project is important because after this trial, Run Configuration (which will be explained later) can identify this project.
Setting for New Class File
Two settings on CRjava2.class file are necessary. After right mouse clicking on CRjava2.class file, select Run As –> Run Configurations.
In Arguments tab, VM arguments is filled as follows (Use copy and paste from aRjava setting).
- VM arguments : -Djava.library.path=C:\Users\shlee\Documents\R\win-library\4.0\rJava\jri\x64
In Environment tab, Add three directories in the following way (Use copy and paste from aRjava setting with buttons).
- LD_LIBRARY_PATH : C:\Program Files\R\R-4.0.3\bin;C:\Program Files\R\R-4.0.3\library;C:\Users\shlee\Documents\R\win-library;
- PATH : C:\Program Files\R\R-4.0.3\bin\x64;C:\Users\shlee\Documents\R\win-library\rJava\jri\x64;
- R_HOME : C:\Program Files\R\R-4.0.3
Now the setting for the added class file is done completely.
Running and Results
When we rerun CRjava2.class file, We can obtain correct results.
We can find that results from only R and Eclipse with rJava are same.
Having done the overall environment setting already, we have only to add two settings on new added class file simply.
From this post, we can make Java code with rJava compact from calling user-defined R functions efficiently. This will help reduce many lines to essencially one line and enhance code readability. \(\blacksquare\)