在Shiny中可视化数据集

时间:2016-03-09 12:30:05

标签: r database graph shiny

我是新手,但我想我知道如何使用网站上的代码从我的数据集中获取我的信息。

我正在开发一个项目,我将传感器数据存储在Phpmyadmin数据库中,现在我只需要提取这些信息并将其显示在反应图中。

 loadData <- function() {
  # Connect to the database
  db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
                  port = options()$mysql$port, user = options()$mysql$user, 
                  password = options()$mysql$password)
  # Construct the fetching query
  query <- sprintf("SELECT value FROM %s", table)
  # Submit the fetch query and disconnect
  data <- dbGetQuery(db, query)
  dbDisconnect(db)
  data // HOW DO I LOAD THIS DATASET
}

但如何将此数据称为绘图?

我改为代码

ui.R

    library(shiny)
library(RMySQL)

shinyUI(fluidPage(

  # Application title
  titlePanel("Visualisatie gegevens"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("plot")
    )
  )
))

因为我只需要在布局和标题中使用图表

server.R

  library(shiny)
    library(RMySQL)
    options(mysql = list(
      "host" = "185.68.145.42",
      "port" = 3306,
      "user" = "user",
      "password" = "******"
    ))
    databaseName <- "user"
    table <- "sensorParser"







 shinyServer(function(input, output) {

      output$plot <- renderPlot({plot(x~y, data = loadData)})

loadData <- eventReactive(input$doQuery, {
      # Connect to the database
      db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
                      port = options()$mysql$port, user = options()$mysql$user, 
                      password = options()$mysql$password)
      # Construct the fetching query
      query <- sprintf("SELECT Value FROM %s", table)
      # Submit the fetch query and disconnect
      data <- dbGetQuery(db, query)
      dbDisconnect(db)
      data
    })

    })

这是我使用的数据库中的数据

id id_wasp id_secret frame_type frame_number sensor value timestamp raw parser_type           
            1 Waspmote 403476432 128 65 FSR 48 2016­03­08 12:26:46 noraw 0
            2 Waspmote 403476432 128 66 FSR 48 2016­03­08 12:26:51 noraw 0
            3 Waspmote 403476432 128 67 FSR 49 2016­03­08 12:26:56 noraw 0
            4 Waspmote 403476432 128 68 FSR 49 2016­03­08 12:27:00 noraw 0
            5 Waspmote 403476432 128 69 FSR 49 2016­03­08 12:27:05 noraw 0
            6 Waspmote 403476432 128 70 FSR 49 2016­03­08 12:27:09 noraw 0
            7 Waspmote 403476432 128 71 FSR 49 2016­03­08 12:27:14 noraw 0
            8 Waspmote 403476432 128 72 FSR 49 2016­03­08 12:27:19 noraw 0
            9 Waspmote 403476432 128 73 FSR 49 2016­03­08 12:27:23 noraw 0
            10 Waspmote 403476432 128 74 FSR 48 2016­03­08 12:27:28 noraw 0
            11 Waspmote 403476432 128 75 FSR 48 2016­03­08 12:27:32 noraw 0
            12 Waspmote 403476432 128 76 FSR 47 2016­03­08 12:27:37 noraw 0
            13 Waspmote 403476432 128 77 FSR 48 2016­03­08 12:27:42 noraw 0
            14 Waspmote 403476432 128 78 FSR 49 2016­03­08 12:27:46 noraw 0
            15 Waspmote 403476432 128 0 FSR 38 2016­03­08 12:27:52 noraw 0

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效。将其添加到server.R并在doQuery

中添加操作按钮plotOutput("plot")ui.R
loadData <- eventReactive(input$doQuery, {
  # Connect to the database
  db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host, 
                  port = options()$mysql$port, user = options()$mysql$user, 
                  password = options()$mysql$password)
  # Construct the fetching query
  query <- sprintf("SELECT value FROM %s", table)
  # Submit the fetch query and disconnect
  data <- dbGetQuery(db, query)
  dbDisconnect(db)
  data
})

output$plot <- renderPlot({
  plot(x ~ y, data = loadData())
})