Running R source code from gvSIG 2.3 through Renjin

One of the new features included in gvSIG 2.3, that was commented at the 11th International gvSIG Conference (there’s also a question from an attendee at the end of the video), is the possibility to run R source code from the Scripting module. To run these R scripts, the Renjin library will be used. We’re going to tell about how to do it.

Renjin is an R interpreter made on Java, but it’s not exactly the native R that we know. It allows us to run code on R and work with all the gvSIG API on Java at the same time. This library has several benefits since it’s completely built-up in gvSIG because it supports Java libraries, but it also have some limitations. One of them is that not all the R libraries are available to be used with Renjin.

“Using Rejin, we have a great integration with gvSIG, but less integration with R”

You can find more information about Renjin in their website, as well as the R libraries, that are available to be used. Renjin is actively in development, so that we hope that there are improvements in the future, about compatibility as well as in functionalities.

We are going to show you a pair of examples. We have to open gvSIG and go to Tools – Scripting – Scripting Composer, and here we create a new Script (an R script).

We can try to run some typical R source code like this:

main <- function() {
cat( "Hello\n" );
cat( "Hello\n" );
d1 <- c(1,3,5);

x <- sample(c("A", "B", "C"), size = 10, replace = TRUE)
print(as.factor(x))

df <- data.frame(x = seq(5), y = runif(5))
print(df)
}

that will show this text by console:

Running script rtest_000.
Hello
Hello
[1] B A B B A C B A A C
Levels: A B C
x y
1 1 0,869
2 2 0,85
3 3 0,255
4 4 0,497
5 5 0,549
Script rtest_000 terminated.

At the following example we are going to show how a Java class can be imported:

import(java.util.HashMap)

main <- function() {

"Renjin example with Java"
ageMap <- HashMap$new()
ageMap$put("Bob", 33)
ageMap$put("Carol", 41)
print(ageMap$size())
age <- ageMap$get("Carol")
cat("Carol is ", age, " years old.\n", sep = "")

print(ageMap)

}

that will show:

Running script rtest_001.
[1] 2
Carol is 41 years old.
Script rtest_001 terminated.

And finally, and more interesting, we are going to show how to interact with gvSIG, accessing to one of the Views that is open in our project to show its name, importing the classes that we need:

import (org.gvsig.app.project.documents.view.ViewDocument)
import (org.gvsig.app.ApplicationLocator)

main <- function() {

 cat( "Accessing to a View\n" );
 application <- ApplicationLocator$getManager();

 view <- application$getActiveDocument(ViewDocument);
 print(view$name)

} 

It would show this text by console:

Running script rtest_02.
Accessing to a View
[1] "GVSIG VIEW 01"
Script rtest_02 terminated. 

 

We still have to find the best way to work with Renjin and gvSIG, since we still are in a development process of this module.

Any collaboration, or feedback that we get from you is welcome.

This entry was posted in development, english, gvSIG Desktop, scripting and tagged , . Bookmark the permalink.

3 Responses to Running R source code from gvSIG 2.3 through Renjin

  1. Pingback: Running R source code from gvSIG 2.3 through Renjin – GeoNe.ws

  2. Pingback: Towards gvSIG 2.3: R extension | gvSIG blog

  3. Pingback: gvSIG 2.3 is now available! | gvSIG blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s