Using R on an embedded device might sound like a wacky thing to do but for "small scale data analytics" and for learning to analyze real-world data (e.g. using JAGS or STAN) it's a good match.
Yes, compared to a PC it's relatively slow - but consider this:
- it runs on small power budget (maybe 0.5W when idling)
- it has a hardware watchdog which makes it more likely that it can run for many months unattended
- Raspbian-lite is based on Debian and it comes with all kinds of well tested tools and applications
I'd like to call it an "LPC" (Low Performance Computing) node, which, like HPC, has its niche - even if it's a much smaller one ;-).
A Raspberry Pi Zero W can be commissioned without ever connecting a monitor or keyboard to it. My starting point was this page but I'm used to the Linux command line and I don't like using something as complicated as "Edger" when dd
is sufficient.
Using your Linux PC first download Raspbian Lite, e.g., bullseye, and unzip it to /tmp
.
Copying the .img
file to a large enough SD card (at least 4GB for lite) using the SD-card block device (in my case /dev/mmcblk0
) works with the following command:
sudo dd if=/tmp/2021-10-30-raspios-bullseye-armhf-lite.img of=/dev/mmcblk0 status=progress bs=4M
After dd
has finished wait a little ((or flush the device) and remove the sd-card from your cardreader and re-insert it. Both boot
and root
should now be mounted.
Should that fail and the dd
command is suspiciously fast, which happened to me once, the problem might be a stale block device. Remove the SD-card, run sudo rm dev/mmcblk0
, re-insert the card and repeat the dd
.
At this point SSH can be enabled and WiFi configured. On my Ubuntu 20.04 machine I run:
touch /media/thomas/boot/ssh
touch /media/thomas/boot/wpa_supplicant.conf:
For configuring WiFi open boot/wpa_supplicant.conf
in a text editor and paste the following:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}
Of course you need to change WIFI_SSID
and WIFI_PASSWORD
. The country code can be changed later on with raspi-config
. Then unmount the sd-card, put it in the Raspberry Pi Zero W, and connect it to power.
Booting the Pi should take about a minute. Log-in with pi@raspberry
(the default password is raspberry
- better change it right away).
Run sudo raspi-config
and do the following steps:
- change the password
- change the WiFi country code
- extend the root partition
- enable the watchdog (see next section)
For convenience I made a new user with the same rights as pi
and the same name as my local user:
sudo su
adduser thomas
apt-get update
apt-get upgrade
apt install vim
# use :%s/:pi/:pi,thomas/g to make the new user akin to user pi
vim /etc/group
Now it's best to reboot (e.g. to activate the hardware watchdog).
The information in diode.io's Raspberry Pi Watchdog-HowTo proved to be accurate.
Do the following steps:
sudo su
echo 'dtparam=watchdog=on' >> /boot/config.txt
apt install watchdog
echo 'watchdog-device = /dev/watchdog' >> /etc/watchdog.conf
echo 'watchdog-timeout = 15' >> /etc/watchdog.conf
echo 'max-load-1 = 24' >> /etc/watchdog.conf
systemctl enable watchdog
systemctl start watchdog
Note that on the Pi the hardware watchdog doesn't support watchdog-timeout
> 15.
Controlling the watchog works with the following commands:
systemctl stop watchdog
systemctl start watchdog
systemctl status watchdog
I configured network monitoring for wlan0
, including a ping to my Fritz!Box (experimental).
sudo su
echo 'interface = wlan0' >> /etc/watchdog.conf
echo 'ping = 192.168.178.1' >> /etc/watchdog.conf
To enable "X11 forwarding" enter the following on the Pi:
sudo su
echo 'ForwardX11 yes' >> /etc/ssh/ssh_config
systemctl restart sshd.service
From my laptop computer I use ssh raspberrypi -X -C
for "X11 forwarding with compression" (which speeds things up considerably).
Installing R
is straightforward, especially when R
runs in a terminal multiplexer. I had some difficulties with screen
but tmux
works fine.
sudo su
apt install tmux
apt install r-recommended
Nvim-R is useful as an "R-IDE" that runs on the Pi (my LPC node, but this could as well be a HPC cluster). The terminal windows to the right shows tmux running nvim with the nvim-R plug-in in a TMUX session. The sub-windows from top-left clockwise are: script, object browser and R console. Many R-commands are available as key-sequences, all most of then starting with "\r" (e.g., setting the cursor in the script window to "dat" and typing "\rb" executes "plot(dat); summary(dat)". Putting the cursor on a line and typing "\d" executes the line. The object browser is quite useful - libraries can be browsed, too.
The plot to the left was produced by a Pi but displayed on my laptop computer through "X11 forwarding", the SSH feature enabled above (the data is from here.
For a vim
and nvim
installation, including the Nvim-R-plug-in, follow this Gist.
Be adviced that after installing many packages (e.g. the Tidyverse) the first start can take several minutes (after typing \rf
the textUpdating nvimcom... Building lists for omni completion...
appears).
The Nvim-R docs are very useful but Rohit Farmer's Nvim-R tutorial helped me fill the gaps in my understanding (his write-up is also helps).