DataTable卡在R的“处理中”

时间:2018-12-21 06:34:49

标签: r shiny mqtt dt

我试图将收到的MQTT数据显示为ShinyApp中的表,但是该表卡在“处理中..”中。下面是我的可复制代码。我尝试了其他解决方案,但似乎没有一个适合我的要求。

library(shiny)
library(mqtt)
library(hrbrthemes)
library(tidyverse)

ui <- shinyServer(fluidPage(
  fluidRow(plotOutput('plot'),
  fluidRow(dataTableOutput("telemetry"))
)
))

server <- function(input, output) {

  get_temps <- function(n) {

    i <- 0
    max_recs <- n
    readings <- vector("character", max_recs)

    temp_cb <- function(id, topic, payload, qos, retain) {
      i <<- i + 1
      readings[i] <<- readBin(payload, "character")
      return(if (i==max_recs) "quit" else "go")
    }

    mqtt::topic_subscribe(topic = "/outbox/crouton-demo/temperature",

                          message_callback = temp_cb)



    purrr::map(readings, jsonlite::fromJSON) %>%
      purrr::map(unlist) %>%
      purrr::map_df(as.list)

  }

  values <- reactiveValues(x = NULL, y = NULL)

  observeEvent(invalidateLater(450), {
    new_response <- get_temps(1)
    if (length(new_response) != 0) {

       values$x <- c(values$x, new_response$update.labels)
       values$y <- c(values$y, new_response$update.series)

    }
  }, ignoreNULL = FALSE)

  output$plot <- renderPlot({
    xdf <- data.frame(xval = values$x, yval = values$y)
    ggplot(xdf, aes(x = xval, y=yval)) + 
      geom_line() +
      geom_point() +
      theme_ipsum_rc(grid="XY")
  })

  show_df <- reactive({

    xdf1 <- data.frame(xval = values$x, yval = values$y)

    return(xdf1)
  })

  output$telemetry <- renderDataTable({show_df()})

}

shinyApp(ui = ui, server = server)

plot似乎工作正常,但无法显示数据表。尝试过library(DT),但没有运气。

0 个答案:

没有答案