If connecting data to the real world is the next sexy job, then how do I do this? And how do I connect the real world to R?
It can be done as Matt Shottwell showed with his home made ECG and a patched version of R at useR! 2011. However, there are other options as well and here I will use an Arduino. The Arduino is an open-source electronics prototyping platform. It has been around for a number of years and is very popular with hardware hackers. So, I had to have a go at the Arduino as well.
|My Arduino starter kit from oomlout|
The example I will present here is silly - it doesn’t do anything meaningful and yet I believe it shows the core building blocks for future projects: Read an analog signal into the computer via the Arduino, transform it with R through Rserve and display it graphically in real time. The video below demonstrates the final result. As I turn the potentiometer random points are displayed on the screen, with the standard deviation set by the analog output (A0) of the Arduino and fed into the
rnormfunction in R, while at the same time the LED brightness changes.
I don’t claim to be an expert in any of this (far from it), but at the end my experiment worked. I am happy to share my experience, as I have benefited from reading various blog posts as well. I can’t guarantee that any of this will not harm your hard- or software. Hence, I used an old computer. Of course feedback will be much appreciated if gently delivered.
Here is a list of hardware and software I used and 10 steps to recreate the experiment:
- Arduino (Uno)
- Computer (2003 iBook G4, MacOSX 10.4.11)
- USB cable
- Potentiometer (10k Ohm)
- Resistor (560 Ohm)
- Arduino (1.0.1)
- Processing (1.5.1)
- Arduino library for Processing
- R (2.15.0)
- Rserve (0.6.8)
Step 1Install Arduino, Processing and R on your computer. I have used my old iBook G4, but the software is available for most platforms, including Windows and Linux.
Step 2Assemble the hardware with the potentiometer, resistor and LED on a bread-board next to the Arduino. This setup is basically example 8 of my oomlout starter kit, with the difference that I connected the LED to Pin 9 instead of Pin 13. You find a detailed description, including video and code on their site.
|Image of my setup created with Fritzing|
Step 3Connect the Arduino with your computer via USB and start the Arduino software. You can reset the Arduino with a little sketch, if you want to:
Copy and paste the code below into a new Sketch and hit the upload button. The LED should light up and as you turn the potentiometer the brightness should change. While you upload the file to the Arduino notice the connection path of your device in the bottom of the console window, mine said: Arduino Uno on
Open the Serial Monitor in the Arduino software (Tools > Serial Monitor) and observe how the displayed numbers change from 0 to 1023 as you turn the potentiometer.
Step 4Close the Arduino software and start Processing. You should get a similar window and IDE as with the Arduino software. Open the preference settings and find out where Processing stores the Sketches by default; mine are saved here:
To make Processing talk to the Arduino it needs the Arduino library for Processing. Download the zip-file and extract it. Expanding the archive should result in a folder called
arduino. If it doesn’t exist already, create the folder
librariesin the default Processing Sketch folder, and place in there the
arduinofolder. Restart Processing, click on File > Examples … and a little window should list the new arduino library at the bottom.
Step 5Check the port number of the Arduino in Processing: The following little Processing sketch will list the serial ports in the console. Match the device paths listed against the one you noted in Step 3 and keep a record of the port number of the Arduino device path.
Step 6Test that you can read and write to the Arduino from Processing with the Sektch below. Change
mySerialPortto the device number you identified in the previous step. Run the Processing code and you should see the potentiometer values displayed in the console window below your code. If you get any error messages regarding the RXTX library (most likely on a Mac) then check out the post by Ellen Sundh.
Step 7Open R and install the Rserve package, e.g. via the command
install.packages(“Rserve”). Start R, load the Rserve package and start the daemon, e.g.
or better via the command line:
R CMD Rserve
Step 9Download the files REngine.jar and RServeEngine.jar from the Rserve page into a temporary folder. Those files are needed to connect Processing to R. More information around how to call R from Processing has the following post on the GuruBlog.
Step 10Back to Processing. Create a new sketch in Processing, insert the code below and save the file. Add the two jar-files you downloaded in the previous step to your new sketch in Processing via Sketch > Add to File …
Fingers crossed. Hit the run bottom and a little window should come up, plotting random points from left to right and as you turn the potentiometer the vertical range of the plotted points should change as well.