[R] Using Java methods in R

Shekhar shekhar2581 at gmail.com
Sat Apr 23 19:35:13 CEST 2011


Hi Hill,
I just finished interfacing the C++ with R, i.e. c++ functions from R
and vice versa. Next thing in pipe line is Java and python.
I just wanted to share some of the things which i think might be
useful for you. Reason being whether its a C++, java or any other
compiled language the interface has to be generic in R. That means
mode of interfacing might be different(using different packages Rcpp
for c++, rJava for Java) but the underlying mechanism remains the
same.

For C++ you will be creating a dll ( windows) and .so( *nix) and for
jave you will be using class.
Now for using c++/java functions with in R or vice versa, you first
should check whether the functions are properly loaded in the R symbol
table or not?

If you are unable to call the java functions in R, then probably you
are unable to load the class into the R environment.

A few steps which i have followed in C++ ( i think must be more or
less same conceptually) could be useful for you:

1) I have a c++ file, i compile it using the command R CMD SHLIB
filename.cpp. On successful completion it creates the library. if you
are getting compilation error then you need to check whether the
header files are in proper path or not.

2) Then for using this function is first use to load the library using
the function dyn.load("library path")

3) If the loading is successful i check whether the required function
is properly loaded into the R symbol table or not by using the
function is.loaded("function name")

4) if it returns true, then i call the function by using .C or .Call
function provided by R.
For more information you can see http://groups.google.com/group/brumail/browse_thread/thread/a278dcbb6a8a439a
.

Hope it helps you in someway.

Regards,
Som Shekhar



More information about the R-help mailing list