Skip to content

Any examples of rpigpior with Shiny? #5

@dcaud

Description

@dcaud

I'm wondering whether Pi's GPIO would work well with R's Shiny.

  1. Could you continuously monitor an input from a GPIO and continuously plot it within Shiny? Perhaps something like:
library(shiny)
library(rpigpior)
library(ggplot2)

# Initialize GPIO
rpigpior::setup(pin = 2, mode = "in")

ui <- fluidPage(
  titlePanel("Real-Time GPIO Pin Monitoring"),
  mainPanel(
    plotOutput("pinPlot")
  )
)

server <- function(input, output, session) {
  
  # Function to read pin status
  readPin <- reactivePoll(1000, session,
                          checkFunc = function() {
                            Sys.time()  # Forces reactive to execute every 1 second
                          },
                          valueFunc = function() {
                            rpigpior::read_pin(pin = 2)
                          })
  
  # Generate plot for output
  output$pinPlot <- renderPlot({
    data <- data.frame(
      Time = Sys.time(),
      Value = readPin()
    )
    
    ggplot(data, aes(x = Time, y = Value)) +
      geom_line() +
      labs(title = "GPIO Pin 2 Status", x = "Time", y = "Value") +
      ylim(0, 1)  # Since GPIO pin values are either 0 or 1
  })
}

# Run the application
shinyApp(ui = ui, server = server)

Also, I wonder how this could be adapted to monitor a momentary switch -- something with short pulse that would not span the polling duration set in the Shiny app.

If anyone has examples (or can just say that this generally would work) would be super helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions