Ant Compile With rJava library

This is pretty simple but for the sake of “I-will-not-forget-it-anymore” I will write it here. I copied some files of a Java project to a new machine. When I tried to execute it in the new place, “Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad version number in .classa file”. Recompiling everything should make it work. Since my code requires rJava it is necessary to inform this to the ant (I’m using ant by the way).  The command which made my day is:

ant clean compile -lib ~/R/i486-pc-linux-gnu-library/2.9/rJava/jri/JRI.jar

2.9: be aware about the version you’ve installed, if you’ve compiled the packages in a version < 2.10, you will need to reinstall ’em. In my case I did reinstall nlme, mgcv, spatstat, and igraph.

Advertisements

cannot mkdir R_TempDir (não é possível criar um diretório em R_TempDir)

I’m constantly using rJava/JRI for a large amount of simulations. Repently it was not working anymore, and since I’d not change anything in the main code, it was a quite weird behavior. My rJava binding looks like this and the first line was the only being executed:

System.out.println("Creating Rengine (with arguments)");
re = new Rengine(args.split(" "), false, null);
if (!re.waitForR()) {
System.out.println("Cannot load R");
return;
}
System.out.println("R loaded");

That was awkward so I tried to use R in the console. When I execute the “R” command I received “cannot mkdir R_TempDir”. So I dug into the R source code to know when it happens. So I figured out that R tries to read environment variables TMPDIR, TMP, and TEMP before set “/tmp” as default one. Since this variables were not set, the “/tmp” is the destiny of the temporary stuff.

When I saw my “/tmp” directory was full, I mean, filled with the maximum number of directories possible in an Ext3 File System, 32K. I realized that all the problem was that R was not getting space for the new “Rtmp…” directories.

To solve that, “rmdir Rtmp*” inside “/tmp” dir. And the world is a happy place again.

Adding New Column To A Data Frame in R

That is a quite noob tip and me, as a noob in R, was pleased to do that. Let’s say you have a data.frame named “saida” with four columns “A”, “B”, “C”, “D”.

>saida # let's see the content of our data.frame "saida"

    A   B    C    D

1   1   64   20   0
2   2   64   20   3
3   3   64   20   3
4   4   64   20   1
5   5   64   20   4
>saida["MY_NEW_COLUMN"] <- NA # That creates the new column named "MY_NEW_COLUMN" filled with "NA"
>saida$MY_NEW_COLUMN <- saida$C - saida$D  # As an example, the new column receives the result of C - D
>saida # let's see the result

    A   B    C    D   MY_NEW_COLUMN

1   1   64   20   0   20
2   2   64   20   3   17
3   3   64   20   3   17
4   4   64   20   1   19
5   5   64   20   4   16

If you want to add many columns, one way to accomplish that is using a list with the names of the new columns, e.g.,:

saida[c("E","F","G","H","I")] <- NA # adds those many columns labeled from E to I.

I am sure there is a more elegant way to accomplish the same result but this is the only way that I know.