Mark Sellors | The worlds smallest R environment? Running R on a $15 computer | RStudio (2022)
Is it possible to do meaningful work in R on a $35 computer? How about a $15 computer? And what does that mean for education, data science, and computing on small devices? In this talk, Mark introduces the R4Pi.org project, its background and why on earth you'd even want to run R at the opposite end of "scale" from everyone else. Session: Unexpected uses of R
image: thumbnail.jpg
Transcript#
This transcript was generated automatically and may contain errors.
Hi, my name's Mark, I'm the founder of the R4Pi project and I'm really excited to speak to you about it today. I hope you're enjoying the conference so far. The R4Pi project exists to make it easier to run R on the Raspberry Pi family of computers. It's been possible to run R on them for a really, really long time, but the experience is not necessarily always a smooth one. So the R4Pi project really aims to kind of smooth that process out to make it a lot easier for folks to get started. We've got full documentation over at r4pi.org and I'm going to show you some more information about the project today.
What is a Raspberry Pi?
So what is a Raspberry Pi? The Raspberry Pi computer is a family of computers that was first launched in 2012, so about 10 years old now. They're created by an organisation here in the UK called the Raspberry Pi Foundation. The foundation's aim is really to improve outcomes for folks through computer science and education and things like that. And the way that they do that is they create a very, very low cost computer. They started out with a computer that cost $35. It's about the size of a deck of cards. And then there've been multiple iterations of that over the years. And then at some point they released a smaller version of that computer, which only cost $15. So now there are probably three main members of the Raspberry Pi family. We'll take a look at those in a little more detail.
The Raspberry Pi 4 Model B is the most common of the Raspberry Pi family at the moment. It's the newest kind of standard size version of the computer. This particular one is in a nice rainbow case, but the computer itself, you can see how small it is. It's probably roughly the size of a deck of cards, something like that. They start at $35. That's the 1GB model. They go all the way up to 8GB of RAM. It's a 4-core CPU at 1.5GHz. There's an SD card slot underneath for storage and all the things you would expect from a modern computer. So USB 3.0, Wi-Fi, Bluetooth, HDMI out, that kind of thing.
Next we have the Raspberry Pi Zero 2W. This is the second iteration of the Zero size computer. You can see that that one is just over an inch wide, a little larger than a stick of gum, I guess. These are the $15 computers. So it has a 4-core CPU at 1GHz, 512MB of RAM, Wi-Fi, Bluetooth, HDMI out, and again, the SD card for the operating system.
Then finally, we have the largest of the Raspberry Pi computers, but that's only because it's built inside this keyboard here. So you can see the keyboard has similar slots to the Raspberry Pi 4. It's basically a Raspberry Pi 4 inside a keyboard case.
It's important to remember that the Raspberry Pi is just a computer. It's a small Linux computer, and as such, works very similarly to most other computers, particularly computers using the Linux operating system.
The R4Pi project
So what about the R4Pi project? What is it, and what does it do?
So the R4Pi project provides pre-built binaries of the R language, first of all, but also provides a CRAN-like repository where we make available pre-compiled versions of CRAN packages. Now, we don't do all of CRAN, maybe one day. Right now, we're just doing a subset of CRAN. So we're building somewhere in the region of 250, maybe 300 packages, something like that at the moment, but we're adding more all the time.
The way that this benefits users is that if you're unaware, if you're a Linux user and you use CRAN, CRAN gives you the source code rather than a pre-compiled binary like they do for Windows and Mac. This is great because R knows how to build them and things like that, but it can be very time-consuming. There are often dependencies which aren't necessarily met in your build environment, and so the R4Pi project builds those packages for you and then makes them available. In this regard, it's very similar to RStudio's package manager, but obviously on a much smaller scale.
With those packages available, it becomes much easier for R users on the Raspberry Pi to install those packages. The installation is much quicker and there's no build that needs to occur. And from there, people can get started much more quickly with R itself without having to worry about all of this build stuff.
Demo: running R on the Raspberry Pi
Okay, so let's take a look at a couple of examples. In this first one, I'm going to log in to the Raspberry Pi via SSH from an iPad. So this is my little kind of desktop iPad setup that I've got going on. I'm going to use a program called blink to SSH into the Raspberry Pi. This basically gets me a kind of Linux terminal, but on the iPad, a butterfly flew into my office and I've typed the password wrong as a result. Okay, so we're logged in and we're going to run R. It looks exactly like R does everywhere else. What we can do though is if we look at options repo, so if we look at the repository that's set, our kind of default CRAN is actually packages.r4pi.org, which is the CRAN-like repository that we maintain as part of the R4Pi project. So that repository contains the pre-built binary packages that we've talked about, and when you install them, it does what R usually does, which is ask you if you want to create a personal library if you haven't already done so. Then it will download those packages from R4Pi.org.
Then once the packages are downloaded, you'll see that it says installing binary package curl and installing binary package rcpp and so on. This is different to what would happen if you installed those packages from CRAN. If you installed them from CRAN, it would say installing source package curl and so on, and it would try and compile those packages on the Raspberry Pi, which can be a very time-consuming process. Obviously, there are occasionally build dependencies and things like that, which we've taken care of for you with the R4Pi project. This makes installation a lot faster, and then once the packages are installed, you can load them as you would normally.
In this example, you see the Raspberry Pi desktop, so this would be particularly useful if you're using one of the larger Raspberry Pis, Raspberry Pi 4, Raspberry Pi 400. We're actually going to do some work inside an IDE. In this case, we're using Visual Studio Code, which is available in the default Raspberry Pi repositories. Once that's fully loaded, you'll see that we have the R extension installed, and that the R extension makes it possible for us to do things like run individual chunks in this R Markdown document, for instance. In this case, it's just running that chunk now, and you'll see that the results come up in a window on the side, in a pane on the side, I guess. And you can knit the whole document from here as well, so just hit the knit button, it's knitting the example, and then once that's knitted, we can go and take a look at it. And it looks exactly as you would expect it to. It's a very basic R Markdown document, obviously, but hopefully proves the point.
Then we can also flip over to this slightly more complex example. This is a really, really nice ggplot2 plot example that I've borrowed from the Palmer Penguins package website. And if we just step through that with control and return, as you would normally, step through, and then when we go for the last step, it will print the plot in the window on the side. We're quite zoomed in here for display purposes, but as you can see, when we actually look at the plot in more detail, it looks exactly as you would expect it to look.
Demo: running a Shiny app
Okay, in this last example, we're going to take a look at a Shiny application. So this is the Shiny application, fairly standard stuff. And we're going to run that directly from the terminal on the Raspberry Pi. So to do that, we use the Shiny's built-in function, which is just run app, so shiny double colon run app. And then when we hit return on there, the application will start. Now this is only listening locally at the moment on a random port, but if we go into the Raspberry Pi's web browser, we can take a look at what that application looks like. So we put in local host, port number, and there it is. So it's a BASTA fusion model visualization. The application is not hugely important.
The point here, as much as anything, is that the R4Pi project enables you to do the things that you can do everywhere else, simply and easily on the Raspberry Pi as well. So it's a standard kind of application. There are some inputs which you can manipulate, which change the outputs a little bit. And that works exactly as you would expect. But what if you want to run that persistently? You want to run that, say, on your home network?
The point here, as much as anything, is that the R4Pi project enables you to do the things that you can do everywhere else, simply and easily on the Raspberry Pi as well.
Well, the Raspberry Pi can help with that as well. Because of the Raspberry Pi's low power draw requirements, they make ideal servers to run kind of 24-7. So the way that we would do that for a Shiny application is we would switch back to the terminal. We would drop out of the running Shiny application here, so we'll stop that. And then we'll run it again, but this time with a couple of extra parameters. So we're going to set the host to 0.0.0.0. That's basically just a way of telling it to listen on the network rather than just locally. And then we'll set a port so that it's easier for me to find later on.
Once that's up and running, we can test it. We can test to make sure that that's available on the network. So to do that, I'm going to flip over to my iPad. And then once on the iPad, I'm going to type in the IP address or the host name, if I have one, of the machine on the local network. I'm using the IP address here. And then I'm going to put in the port that we specified earlier on. And we'll see if that loads. And there we go. It's the exact same BASTAfusion model visualization running in the exact same way as before. The rendering is probably a little different because we're using the iPad web browser rather than the one that's built into the Raspberry Pi. But other than that, it works in exactly the same way. The same inputs connected to the same outputs that change the plots in exactly the same way.
Edge computing and the possibilities of small devices
One of the things that I'm most excited about about running R on a Raspberry Pi is because it's a very small computer, you can run them in places where you wouldn't normally run a $1,000, $2,000 computer. They can be powered by batteries in a lot of cases, especially the smaller ones. You can run those from a USB battery bank. But you can imagine ordinarily with data science, people give us data. So the data is collected somewhere. It's bundled together, often not very well, and then handed over to the data scientists for analysis. But what we can do when we have a small computer like the Raspberry Pi, is we can put that computer where the data is collected. So that could be the data collection device as well as the place where the analysis occurs. This kind of concept in computing is often referred to as edge computing because you can take the centralized resources that are often used for this sort of work and you can move them to the edge of the network. This means that you can run projects in a lot more interesting places than you might otherwise be able to.
Because of the low power requirements of the Raspberry Pi, you can run them in a car while you're out power sending. I can imagine people running them while they're, you know, on a building site or in a digger. Maybe even a farmer looking after cows in a field, in a forest, on a boat. The possibilities really are endless with such a small low power device.
There are many different ways that you could run the types of projects that we've talked about here today. You don't need a Raspberry Pi computer to do any of this stuff. But for me, the kind of low cost, the kind of ease of entry into the ecosystem is a really big boon for a certain class of users. Whether they're people who are just, you know, coming to R for the first time, just want to learn. People want to learn Linux so that they can use R on Linux. Because obviously that's more or less a de facto operating system for scientific and technical computing at this point. Or for any other reason. Just having something that's very low cost and very accessible for folks is a really nice way to help people get into the R ecosystem.
Just having something that's very low cost and very accessible for folks is a really nice way to help people get into the R ecosystem.
Thanks for watching the talk to this point. Please check out the r4pi.org website, check out the Raspberry Pi family of computers if that's something that you think you might be interested in. My personal website and Twitter details are there as well if you want them. Thanks again and I hope you have an excellent rest of the conference. Bye.