R on a Raspberry Pi

Mark Niemann-Ross

R on a Raspberry Pi

Mark Niemann-Ross * mark@niemannross.com

  • |>

  • R*aspberry[pi]

  • !!

def fib(n):
     a, b = 0, 1
     while a < n:
         print(a, end=' ')
         a, b = b, a+b
     print()
 fib(1000)

Raspberry Pi

An image showing the Raspberry Pi Model 4

Raspberry Pi Model 4

  • Linux / Python

  • ~ $45 USD (depending on model)

  • consumes ~3-6 watts

  • USB, HDMI, Ethernet, GPIO, camera port, display port, audio

Install R

sudo apt-get update
sudo apt-get install r-base r-base-core

Fix the App Menu

Image showing how to fix the app location in Raspbian

Fix the Icon

Image showing how to fix the icon for R

Install a gui

An image showing the Rgui console. An image showing a screen capture of the Rstudio IDE
An image showing the Geany IDE posit Cloud

Setup geany

An image showing how to configure Geany to run the R programming language

Configuring geany for R

Run R

Saved as “hello_world.R”

# install.packages("cowsay")

library(cowsay)

say("Hello world")

plot(x = 1:10, y = 10:1)

Install libgpiod

One of many libraries for GPIO access

sudo apt-get install libgpiod-dev gpiod

Other libraries:

Read the GPIO

Schematic showing how to connect a pushbutton to Raspberry pi

Read the GPIO

# assign buttons 
TRYME_btn <- 21 # BCM numbering scheme 

while (TRUE) { 
   getGPIOcmd <- paste("gpioget -l gpiochip0", TRYME_btn) 
   if (system(getGPIOcmd, intern = TRUE) == 0) print("Try Me!") 
} # control-Z stops the loop

1 Available on github as hello_gpio.r

Control the GPIO

schematic to control light bulb with Raspberry Pi

Control the GPIO

# assign buttons (use BCM - not board)
getGPIOcmd <- "gpioget gpiochip0 21"

# assign control pin (use BCM)
setGPIOcmd_on <- "gpioset gpiochip0 9=1"
setGPIOcmd_off <- "gpioset gpiochip0 9=0"

while (TRUE) {
  if (system(getGPIOcmd, intern=TRUE) == 1) print ("Try Me!")
  
  system(setGPIOcmd_on)
  Sys.sleep(1)
  system(setGPIOcmd_off)
  Sys.sleep(3)
}

1 Saved as hello_gpio_control.R

Device Drivers

Problem: Drivers for circuits are written in other languages.

Solution: R works with other languages