R on a Raspberry Pi Zero W

rstats
raspberrypi
machinelearning
Running R on a tiny, $10 computer.
Published

July 26, 2022

If you know me, you should know that I am obsessed with tiny computers and that some time ago I got myself a Raspberry Pi Zero W on which I run Pi-hole to block ads and tracking within our home network. How tiny is a Pi Zero W, you might wonder? Well, here it is:

A Pi Zero W.

And yes, that is a 13-inch MacBook Pro in the back and that’s a microSD card for storage:

A Pi Zero W.

I mean, look how tiny and cute it looks in its red-and-white case:

A Pi Zero W.

The whole case is barely thicker than the MacBook Pro itself, while the board is exactly 65 by 30 mm. It is, of course, not a powerhouse, being powered by a single-core, 1 GHz, 32-bit CPU with 512 MB of RAM, but that is more than enough for Pi-hole and some tinkering. And did I mention that it costs only $10? No? Well, isn’t that great?

Anyway, what you might not know is that, being powered by a full Debian-based distribution, you can install a variety of software using the apt package manager. Including R which, however, is often lagging behind the latest CRAN release (I could only install R 3.5.2 using apt).

Interestingly, a couple of days ago while I was browsing the schedule for rstudio::conf(2022), I came across the R for the Raspberry Pi project. It aims to provide up-to-date builds of R for Raspberry Pi computers, which can be installed in few simple steps. I mean, I obviously had to check it out!

A few moments later...

…here we are, connecting to the headless Pi via SSH on my macOS terminal:

Screenshot from the Pi Zero W running R 4.1.2.

Nice, even though the latest available version (for my Pi Zero W, at least) is only R 4.1.2.

The next obvious question is: can you actually do something with R on such a low-powered computer?

To answer this question, I set up a short benchmark script that simulates data from a logistic regression model, for 1,000 subjects, and then fits the corresponding, true model 100 times (using the {microbenchmark} package). The script is available in the following Gist if you want to try running it on your machine for comparison:

Here are the results:

A Pi Zero W.

The median time was 106.4 milliseconds, with an interquartile interval of 105.9 to 118.4 milliseconds. Not bad! For comparison, let’s run the benchmark script on my 2019, 13-inch MacBook Pro (2.4 GHz, quad-core i5 processor, an i5-8279U, with 16 GB of RAM). That can be done in just a couple of lines, directly from your R session:

gist_url <- "https://gist.githubusercontent.com/ellessenne/5e0c35a7a0625d9dd5bbe8c284d52155/raw/9392820a5f53d8f438a19f1518e30eeca1df30f3/pizw-bench.R"
source(gist_url)
Unit: milliseconds
                expr      min       lq     mean  median       uq      max neval
 logistic regression 2.364326 2.817422 3.720309 3.29978 3.823631 12.73771   100

Okay, I guess the Pi Zero W is slow compared to my relatively modern laptop, approximately 30 to 40 times slower… but hey! It’s machine learning on a $10 computer, isn’t that awesome?

It’s clearly not enough for large projects, but a fantastic option for democratising data science, thanks to open-source software and a tiny $10 computer. If you want to read more about the concept, don’t forget to check out this early draft by the team of Jeff Leek on the importance of democratising data science education. The future is bright!