Search code examples
javarfunction-calls

Calling custom R functions from Java


I have look into rJava and checked out a video on RCaller, but in all examples I saw people are only calling native R functions. I have found no way of loading a *.r file and using its content.

The main question is: How to integrate a custom Rscript in Java, so that I can call my custom-made R functions through java?

The thing that I want to do is to create a simple GUI with images loaded from folders, buttons and be able to call my custom R functions on click and dynamically swapping images depending on outcomes(the logic would be in R).


Solution

  • Good practice would be to hide the R code behind an abstraction, i.e., an interface. Once the interface has been defined, call rJavax::setJavaImplementation() to define an R reference class that implements the interface.

    An alternative solution (but not a direct answer to your question) would be to use e.g. the gWidgets package to create your GUI in R. That should be much easier than attempting the same with Java/Swing.