Calling R from Processing

Nikolaus Gradwohl2010-08-10T20:31:01+00:00

Processing is a simple and powerfull programming language to create images, animation and interaction.

R is a powerfull free software environment for statistical computing.

For me this sounds like a perfect match. This is a short howto that shows how these two languages can comunicate with each other.

On the R-Side I use the Rserve extension, which can be installed by calling


from within the R environment.

By calling


from within the R, you can start a serverprocess that accepts commands on a network socket and can be used by a javaclient to execute R commands.


In the processing sketch we need to add the two jar files of the javaclient that can be downloaded at the Rserve download page

just add the jar files to the sketch using "Add file ..." from the "Sketch"-Menu.

Now we can use the REngine class to execute some R code in the server process and ask R to return the calculated data

The following processing sketch ask R for 100 normal distributed randoms and then sort them. Then the data is fetched into an array of doubles and displayed in the draw method.

import org.rosuda.REngine.Rserve.*;
import org.rosuda.REngine.*;

double[] data;

void setup() {

  try {
    RConnection c = new RConnection();  
    // generate 100 normal distributed random numbers and then sort them 
    data= c.eval("sort(rnorm(100))").asDoubles();

  } catch ( REXPMismatchException rme ) {

  } catch ( REngineException ree ) {

void draw() {
  for( int i = 0; i < data.length; i++) {
    line( i * 3.0, height/2, i* 3.0, height/2 - (float)data[i] * 50 );
Tweet This! submit to reddit Digg! Tags: | 7 comments | no trackbacks

See also:

a circular chart im Processing
sine-function experiment
projecting GIS Data on a sphere using Processing
processing ical-flowers-2.0
Training Bike Visualization



Leave a response

  1. Mike 2011-05-31T04:03:39+00:00

    Can this work the other way i.e. have R call Processing as required? I'd like R to do some calculations and then call Processing for some more esoteric displays, but R has to initiate the call.

    P.s.- I 'get' Matlab/R but am just a Java noob (hence Processing) and know nothing really about the 'serve' portion.

  2. Josh 2012-02-21T10:51:15+00:00

    Very cool. Can you say anything more about getting R process up and running?

    If I do this all manually, opening a terminal window, and starting R, and doing "library(Rserve", and "Rserve()", and THEN from within Processing run your program, all is well.

    However, it appears that it is also possible to start Rserve from the command line using

    R CMD Rserve

    but when I did this, it told me that Rserve was not found:

    noname:~ josh$ R CMD Rserve /Library/Frameworks/R.framework/Resources/bin/Rcmd: line 61: exec: Rserve: not found


  3. Reitickintefe 2012-04-30T06:31:55+00:00

    Obedient bye, genial soul mate :)

  4. seth 2012-08-09T19:12:29+00:00

    This is really great, thanks! I plan on using/abusing this a ton.

  5. Diego 2014-08-29T20:10:48+00:00

    hey there, I'm trying to use this basic tutorial to conect processing with R. I copied your code but got this error on Processing:

    org.rosuda.REngine.Rserve.RserveException: Cannot connect: Connection refused at org.rosuda.REngine.Rserve.RConnection.( any ideas on how to fix it?

  6. Nico Wells 2016-06-27T17:25:08+00:00

    I have the same problem as 5. Diego. Any help would be appreciated.

  7. Nikolaus Gradwohl 2016-06-28T06:56:46+00:00

    This sounds like the RServe process is not running. Make sure you start the RServe process before running the processing sketch and don't close the console window where the RServe process is running. On a windows system the firewall might also block access to the server process - can't test that right now

Leave a comment