6

I want to read a data stream from a serial port (USB port).

In my case it's data from an Arduino board, however the issue is generic I think.

This is easily done in R. For instance:

library(serial)
conn <- serialConnection("arduino", port="ttyUSB0", mode="9600,n,8,1") # port may be different on other machines and OS's
open(conn)
while(TRUE) { print(read.serialConnection(conn) ) }

This will continuously spit out the data stream as text (including a lot of NULL's).

I want to do something similar in a Shiny App. I have not managed to get this working despite an excellent introduction to R/Shiny and data streams here: link. There is a minimal non-working example below.

The reported error is: argument 1 (type 'closure') cannot be handled by 'cat' which has been confused. Is it a data type problem that print() can handle, but renderText() can not?

library(shiny)
library(serial)

conn <- serialConnection("arduino", port="ttyUSB0", mode="9600,n,8,1")
open(conn)

ui <- fluidPage(
  titlePanel("Shiny Data Serial Stream"),
  fluidRow(
    column(4, 
       hr(),
       textOutput('serial'),
       hr()
      ) 
    )
  )

server <- function(input, output, session) {
  serialRead <- reactive({
    invalidateLater(100, session)
    read.serialConnection(conn)
  })

  output$serial <- renderText({serialRead})
}

# Run the application 
shinyApp(ui = ui, server = server)
gre_gor
  • 6,669
  • 9
  • 47
  • 52
Søren ONeill
  • 355
  • 2
  • 13

0 Answers0