Ignore NA/NAN in Shiny App Datatable and plotly

I am writing a Shiny app for a serial port interface, and for the most part the app is working EXCEPT that sometimes the hardware sends incomplete data or my inactivatelater is a little off. So NA and sometimes NAN are produced. This would not be a problem if I could filter out the NA/NAN from the reactive DATATABLE. I have put na.omit everywhere I can think of but it still shows up in the DATATABLE. How do I get Shiny to ignore the NA and NAN in the table and plotly. Do I need to write a if else statement to make the reactiveVal() NULL when an NA is encountered? And how do I get Plotly to ignore them as well? {r} rv <- reactiveVal()

server <- function(input, output, session) {

  comm <-reactive(
    as.character(input$comm)
  )
  conn <-(serialConnection(name="Prolific USB-to-Serial Comm Port(Com3)",
                          port="COM3",
                          mode="9600,n,8,1",
                          newline=0,
                          translation="lf",
                          handshake = 'xonxoff')
)

  error_clock<- reactive({
    error_clock <-proc.time()
    error_clock
    })

  serialRead <-reactive({

    invalidateLater(900, session)
  tryCatch(
      expr = {
        read.serialConnection(conn)

      },
      error = function(e){
        message('Caught an error!')

        flush(conn)
        #read.serialConnection(conn)
        invalidateLater(400, session)
        read.serialConnection(conn)
       # proc.time()-error_clock


      },
      warning = function(w){
        message('Caught an warning!')
        flush(conn)
        invalidateLater(400, session)
        read.serialConnection(conn)
        open(conn)

      }
    )

  })
  time1 <-reactive({
    ptm <-Sys.time()
    for(i in 1:15)
      if(!is.na(serialRead())){
    ptm}else{
      ptm<-NULL}
     ptm
     format(.POSIXct(ptm), "%H:%M:%S")
    })

 # output$serial <- renderText({
  #     serial <-if(str_detect(serialRead(),"rxDone", negate=TRUE)){
   #                    print("Retrieving Power")}else{
    #                  na.omit( captureRxData(serialRead()))
  #                     }
   # })



 dBm1<-reactive({

   if(str_detect(serialRead(),"xdata:", negate=FALSE)){
                 mean(na.omit( captureRxData(serialRead())))}else{
                 serialRead<-"na"}

   })

   rv <-na.omit(reactiveValues())



   observeEvent(dBm1(),{
     if(length(dBm1())==0 | length(time1())==0){
       return(NULL)}else{
     rv$m<- rbind(m=data.frame(x=time1(),y=dBm1()))
     }
     })



 output$myPlot <- renderPlotly({
    plot_ly() %>%  add_markers(data=na.omit(rv$m),x=~time1(),y=~dBm1()  )
   })


 output$Table <- renderTable(cbind(time=time1(), dBm=dBm1()))


 this_table <- reactiveVal(this_table)%>%
   na.omit()

 observeEvent(rv$m, {
   t= rbind(data.frame(time = time1(),
                        dBm= round(na.omit(dBm1()),2)), this_table())
   this_table(na.omit(t))

 })

 output$shiny_table <- renderDT({
   datatable(na.omit(this_table()), selection = 'single', options = list(dom = 't'))
 })




}

shinyApp(ui,server)

Thanks In Advance


Read more here: https://stackoverflow.com/questions/67928955/ignore-na-nan-in-shiny-app-datatable-and-plotly

Content Attribution

This content was originally published by alittleloopy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: