title: How to read serial data stream into a shiny app
body: <p>I want to read a data stream from a serial port (USB port). </p>
  
  <p>In my case it's data from an Arduino board, however the issue is generic I think.</p>
  
  <p>This is easily done in R. For instance:</p>
  
  <pre><code>library(serial)
  conn &lt;- 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) ) }
  </code></pre>
  
  <p>This will continuously spit out the data stream as text (including a lot of NULL's).</p>
  
  <p>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: <a href="https://rviews.rstudio.com/2017/11/15/shiny-and-scheduled-data-r/" rel="nofollow noreferrer">link</a>. There is a minimal non-working example below.</p>
  
  <p>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?</p>
  
  <pre><code>library(shiny)
  library(serial)
  
  conn &lt;- serialConnection("arduino", port="ttyUSB0", mode="9600,n,8,1")
  open(conn)
  
  ui &lt;- fluidPage(
    titlePanel("Shiny Data Serial Stream"),
    fluidRow(
      column(4, 
         hr(),
         textOutput('serial'),
         hr()
        ) 
      )
    )
  
  server &lt;- function(input, output, session) {
    serialRead &lt;- reactive({
      invalidateLater(100, session)
      read.serialConnection(conn)
    })
  
    output$serial &lt;- renderText({serialRead})
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>