Evolving notes, images and sounds by Luis Apiolaza

Using Processing and R together (in OS X)

I wanted to develop a small experiment with a front end using the Processing language and the backend calculations in R; the reason why will be another post. This post explained the steps assuming that one already has R and Processing installed:

  1. Install the Rserve package. This has to be done from source (e.g. using R CMD INSTALL packagename).
  2. Download Rserve jar files and include them in the Processing sketch.
  3. Run your code

For example, this generates 100 normal distributed random numbers in R and then sorts them (code copy and pasted from second link):

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

double[] data;

void setup() {
  size(300, 300);

  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 ) {
      rme.printStackTrace();
  } catch ( REngineException ree ) {
      ree.printStackTrace();
  }
}

void draw() {
  background(255);
  for( int i = 0; i < data.length; i++) {
    line( i * 3.0, height/2, i* 3.0, height/2 - (float)data[i] * 50 );
  }
}

The problem is that this didn't work, because my OS X (I use macs) R installation didn't have shared libraries. My not-so-quick solution was to compile R from source, which involved:

  1. Downloading R source. I went for the latest stable version, but I could have gone for the development one.
  2. Setting up the latest version of C and Fortran compilers. I did have an outdated version of Xcode in my macbook air, but decided to delete it because i- uses many GB of room in a small drive and ii- it's a monster download. Instead I went for Apple's Command Line Tools, which is a small fraction of size and do the job.
  3. In the case of gfortran, there are many sites pointing to this page that hosts a fairly outdated version, which was giving me all sorts of problems (e.g. "checking for Fortran 77 name-mangling scheme") because the versions between the C and Fortran compilers were out of whack. Instead, I downloaded the latest version from the GNU site.
  4. Changing the config.site file in a few places, ensuring that I had:
  5. CC="gcc -arch x86_64 -std=gnu99"
    CXX="g++ -arch x86_64"
    F77="gfortran -arch x86_64"
    FC="gfortran -arch x86_64"
    

Then compiled using (didn't want X11 and enabling shared library):

./configure --without-x --enable-R-shlib
make
make check
make pdf # This produces a lot of rubbish on screen and it isn't really needed
make info

And finally installed using:

sudo make prefix=/luis/compiled install

This used a prefix because I didn't want to replace my fully functioning R installation, but just having another one with shared libraries. If one types R in terminal then it is still calling the old version; the new one is called via /luis/compiled/R.framework/Versions/Current/Resources/bin/R. I then installed Rserve in the new version and was able to call R from processing so I could obtain.

A 'hello world' of the calling R from Processing world.

A 'hello world' of the calling R from Processing world.

Now I can move to what I really wanted to do. File under stuff-that-I-may-need-to-remember-one-day.

5 Comments

  1. Markus Gesmann

    Hi Luis,
    Great Post! I played around with R and Processing while back on a Mac as well, but was lucky not to run into the problems with shared libraries: http://lamages.blogspot.co.uk/2012/10/connecting-real-world-to-r-with-arduino.html. I haven’t used Processing with R since, so I look forward to your next post for ideas and motivation to get my Arduino out of the drawer again.
    Thanks
    Markus

    • Luis

      You’re most welcome. I’m glad the post was useful for someone else.

  2. Jeff Weakley

    yes, I’m in the process of learning Processing so I can use it with Data. In the back of my mind, I’ve been wondering how the two can connect. Much thanks.

  3. Peter

    Hi Luis,
    I manage my installation of R on my mac using homebrew. Because homebrew builds R from source anyway, it was easy to just add the –enable-R-shlib option to the standard build script by using the “brew edit r” command. Thanks for the interesting post!

    • Luis

      Thanks for that pointer. I may go that route next time.

© 2024 Palimpsest

Theme by Anders NorenUp ↑