I am working on a project which is primarily in R and requires Java to run. So, I need a way to run Java in R itself.
For example, is there any way to run this code in R?
public static void main(String[] args){
for(int i=0;i<5;i++){
System.out.println("HelloWorld");
}
}
** Lib in R (v8,rJava,rserve)
Actually, the first google result for "javascript in r" that I got leads to vignette of js package, that uses V8 package providing V8 implementation of JavaScript for R. You can use either of the packages to run JS code from R.
Related
I have the follow Java code that uses Rcaller.
RCaller caller = new RCaller();
RCode code = new RCode();
caller.setRscriptExecutable("/usr/bin/Rscript");
code.addRCode("install.packages(\"bbmle\")");
caller.redirectROutputToConsole();
caller.runOnly();
Essentially, I'm trying to run Java code that installs an R package (because later I will run R code within Java that requires this package).
When I run this code, I get the following output in Java
Error:Loading required package: Runiversal
However, I do have the Runiversal package on my Mac. Any ideas what this means, and why I'm not actually seeing any R output of the install.packages method, which is normally quite verbose?
UPDATE: I should note that even simple code such as the following results in the same error:
code.addRCode("x = c(1,2,3)");
code.addRCode("y = c(2,3,5");
code.addRCode("x+y");
caller.redirectROutputToConsole();
caller.runOnly();
UPDATE: I should also mention that the following works great:
StringBuffer allCode = readFile("temp.R");
code.setCode(allCode);
double[] xvector = new double[]{1,3,5,3,2,4,5,6,7,8,9,21,22,25,27,25,34,39,31};
double[] yvector = new double[]{6,7,5,6,5,6,6,7,6,8,9,21,20,19,23,24,29,38,30};
code.addDoubleArray("X",xvector);
code.addDoubleArray("Y",yvector);
code.addRCode("fun(X,Y)");
temp.R:
fun = function(x,y) {
return(lm(y~x))
}
Java output:
Output:
Output:Call:
Output:lm(formula = y ~ x)
Output:
Output:Coefficients:
Output:(Intercept) x
Output: 2.445 0.825
Output:
R loads package from the libraries. If the library that you installed the Runiversal package into is not being searched by the R process started under Java, that could result in the error message you're seeing.
So: what is the directory path (i.e., library) that the Runiversal package is installed in? Possibly related to this is what user installed the R package, and what user is running the Java code.
The 2.2 version of RCaller library does not require the R package Runiversal. A compact version of R to XML converters are implemented in the package. Try it out here
OK, again having some problems with caliper.
I am now running on Linux, trying to use the beta snapshot. I am attempting to run Google's caliper via commandline using just the jar. (Beta snapshot)
I do not have access to maven on this machine, and installing it is out of the question. I would just like to use a jar and, maybe once this is working, I can write up a script or something.
Here is what I am doing:
1. Using small example Benchmark:
import com.google.caliper.Benchmark;
public class Tutorial {
public static class Benchmark1 {
#Benchmark void timeNanoTime(int reps) {
for (int i = 0; i < reps; i++) {
System.nanoTime();
}
}
}
}
2. Compile with javac -cp caliper-1.0-beta-SNAPSHOT-all.jar Tutorial.java
3. (Attempt to) run with
java -cp caliper-1.0-beta-SNAPSHOT-all.jar com.google.caliper.runner.CaliperMain Tutorial.Benchmark1, receive message Benchmark class not found: Tutorial.Benchmark1.
I've tried to work this out from bits and pieces of information from various sources but I am really having a heck of a time with this. I would appreciate any input.
I believe you really need no maven, this should work.
Your own class doesn't get found and I think it's a problem of your classpath. As they're usually more problem with nested classes try simply
java -cp caliper-1.0-beta-SNAPSHOT-all.jar com.google.caliper.runner.CaliperMain Tutorial
If the message changes to something like "class contains no benchmarks", then you'll know more. If you insists on using nested class, you may need to call Tutorial$Benchmark1 (unprobable, but possible; java class naming is sick).
Please try also
java -cp caliper-1.0-beta-SNAPSHOT-all.jar Tutorial.Benchmark1
to see if your class lies on the classpath (the message should change to something like "no main method").
See also this older post.
I have written some code in C which I want to call from Java. What I have done is I have a function in my c code which calls another c function from libspotify and I am trying to write a Java Wrapper using JNA to call my c function.
I have written a simple login function for spotify in c which is actually working. Here is a link to that c file which has the method
https://github.com/shroffrushabh/libspotify_java/blob/master/jukebox.c
The next step I followed is, instead of using the make file provided in the libspotify examples I used the following cmd to compile and create a .so file
gcc -o libspot.so -shared jukebox.c appkey.c
This is what my java file looks like
import com.sun.jna.Library;
import com.sun.jna.Native;
public class SpotifyTest {
public interface JukeBox extends Library {
public int login();
}
static public void main(String argv[]) {
JukeBox jk = (JukeBox) Native.loadLibrary("spot", JukeBox.class);
jk.login();
}
}
The following are the commands to compile and run the java file
javac -classpath jna-4.0.0.jar SpotifyTest.java
java -classpath jna-4.0.0.jar:. SpotifyTest
Following is the exception I get:
java.lang.UnsatisfiedLinkError: /home/rushabh/libctest.so: undefined symbol: sp_session_create
I am not sure what is going wrong here, but here is what I am thinking, there is a sp_session_create function in libspotify(the C api which spotify has provided) which I am calling in my c code, so I think I need to somehow link the libspotify library when I am trying to generate my .so file. It would be great if you could give me some suggestions on how to solve this problem
You need to link in the spotify library.
This should be done when you're compiling libspot.so. This can be done with:
-L/path/to/spotify/library
This should be something/libspotify/lib, and is likely the directory where you built libspot.so. You may also need to specify:
-I/path/to/spotify/includes
after 2 days of researching this is how I got it to work, following is the command I used to generate the .so file.
gcc -L~/Downloads/libspotify-12.1.51-Linux-i686-release/lib -shared appkey.c jukebox.c -lspotify -o libspot.so
Rest of the steps to execute to Java file are same as above.
Thank you Erick Robertson and Petesh for your help :)
I have written JNI wrappers to export the API of a C application (G-WAN) which embeds a JVM. The native calls are implemented in the C application and exported with RegisterNatives().
Ideally I would have a 'gwan' class for the G-WAN API:
import gwan // G-WAN API
public class hello {
public static int jmain(long env, String[] args) {
gwan.xbuf_cat(gwan.get_reply(env), "Hello World");
return 200; // HTTP status (200:'OK')
}
}
I would like to do something like the "#import gwan" above to import the native call prototypes, but currently I only have the following (which works):
public class hello {
public static int jmain(long env, String[] args) {
gwan_xbuf_cat(gwan_get_reply(env), "Hello World");
return 200; // HTTP status (200:'OK')
}
public static native long gwan_get_reply(long env);
public static native void gwan_xbuf_cat(long ctx, String str);
}
Again, the implementation of the native calls in made in the G-WAN executable (not in a Java class stored on disk).
Because the G-WAN API is quite large, I would like to have the native call prototypes in their own 'gwan' class (or namespace) if possible (like in the first hello example above).
Any suggestion about how to do that?
(please post Java or JNI code because I am not a Java expert)
Disclamer: I am involved in the development of this project.
I would suggest that you read following paper on JNI from Sun now Oracle
http://java.sun.com/docs/books/jni/html/jniTOC.html
And after that it should be understandable but some pseudocode and its not tested would be to move the two gwanapi calls into its own file named gwanapi.java
public class gwanapi {
public static native long get_reply(long answer);
public static native void xbuf_cat(long ctx,String str);
}
then you compile that file with javac gwanapi.java -> output: gwanapi.class
you type javah -jni for the c/c++ header implementation:
javah -jni gwanapi
the next you should in your hello java class is to call static{ System.loadLibrary("gwanapi");}
Pseudo code and NOT tested
public class hello{
static{
System.loadLibrary("gwanapi");
}
public static int jmain(long env,String args[]){
gwanapi.xbuf_cat(gwanapi.get_reply(env),"Hello World!");
return 200;
}
}
and you should be good to go.
But I might have missed a point or two but I think this is the smallest amount of work you should do.
Oh by the way http://en.wikipedia.org/wiki/Java_Native_Interface is also some form of source for JNI calls and how it works and lead you to more sites with more information.
Thanks
Being a C programmer, I have had to read the C source code of the JVM to find that Java remaps Class (path) names with dots instead of slashes.
Since the G-WAN directory hierarchy uses IP addresses to define listeners and virtual hosts (192.168.10.10_80/#domain.com/csp), those dots were confusing the FindClass() JNI call, making it fail to find the classes.
I also found that the classpath path separator is a ":" for Unix and a ";" for Windows. That was not the cause of my problem, but it might cause the same issue.
Finally, I stopped using the GCJ compiler's JVM because it does not support formating doubles (since at least 2006). Using either OpenJDK or the SUN/ORACLE JVM works as expected.
All works fine now. I post all this here just in case it may help others.
I'm trying to run R using Java. I have R installed on my Mac and I've used it plenty of times from the terminal.
In the terminal, to start R, one simply types "R"
Ex:
Macintosh-11:Desktop myname$ R
R version 2.12.2 (2011-02-25)
Copyright (C) 2011 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
So, what I would like to do is run R through Java, via the terminal. So, I wrote myself a Java class:
public class javar {
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("R");
}
}
However, when I compile and then execute, using
java javar
I don't see R start. I simply see the program finish executing, and then the terminal is ready for another command.
How can I achieve what I'm trying to do?
when I encountered your problem, I ended up using JRI, which not only creates an R/Java connection, but allows you to exchange matrices and vectors to/from the two.
Although I don't suggest it, your approach may work, but R will not "hang" after you launch it, it will just execute nothing and exit. Try adding the "--vanilla" option and obviously feed some code to it, with --file=yourScript.R and eventually arguments using --args
public class javar {
public static void main(String[] args) throws Exception {
Runtime.getRuntime().exec("R --vanilla --file=yourScript.R");
}
}