首页 > 解决方案 > DataTable卡在R中的“处理”中

问题描述

我正在尝试将接收到的 MQTT 数据显示为 a 中的表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)但没有运气。

标签: rshinymqttdt

解决方案


推荐阅读