0

I'm new to shiny but I think I know how I can get my information from my dataset with the code from the website.

I'm working on a project where i store my sensor data in a Phpmyadmin database, now i just need to extract this information and show it in a reactive graph.

 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
}

but how do i call this data for plotting?

SO i changed to code

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")
    )
  )
))

because i just need a graph in my layout and a title

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
    })

    })

this is de data in the database i'm using

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
Maarten
  • 1
  • 2

1 Answers1

0

Something like this should work. Put this in your server.R and add both an action button doQuery and the plotOutput("plot") in the 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())
})
mlegge
  • 6,763
  • 3
  • 40
  • 67
  • Thanks for answering so fast, I hope you don't mind helping me a bit more :), cause my programming skills are bad :-/ so i put all this code on my server.R file, i run the program and this is the error is get : cannot coerce class ""reactive"" to a data.frame Any idea? kind regards maarten – Maarten Mar 09 '16 at 15:45
  • @Maarten I can only guess what is wrong, you need to provide a [minimally reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) if this answer is not sufficient to guide you to your goal – mlegge Mar 09 '16 at 15:48
  • thanks again for the fast reply, i updated my question with (i think is) all the info you need. thanks for the help in advance – Maarten Mar 09 '16 at 16:07
  • @Maarten, take a closer look at the `plot(...)` statement, you need `loadData()` instead of `loadData` – mlegge Mar 09 '16 at 19:39
  • thanks for all the help, there are still some errors in it but i'l get it out :). again thanks alot – Maarten Mar 10 '16 at 09:07
  • hi, i looked at it for a few hours and found my error, i fixed it, but now i don't get any errors, but i don't see my plot. could you take another quick look at it, i've updated the code. thanks in advance – Maarten Mar 11 '16 at 08:37
  • @Maarten you still have `output$plot <- renderPlot({plot(x~y, data = loadData)})` instead of `output$plot <- renderPlot({plot(x~y, data = loadData())})` and the `x` and `y` are supposed to be placeholders -- I don't know what you actually want to plot, but you should use the column names there – mlegge Mar 11 '16 at 16:19
  • thanks again for answering i changed the the code to `output$plot <- renderPlot({plot(data$timestamp ~ data$value, data = loadData())})` but still get no plot. thanks again for the help in advance. I want to see a lifestream from the data thats getting in my SQL database, with the time frame on the x axis and the Sensor value on the y-axis – – Maarten Mar 14 '16 at 09:45
  • I recommend you spend some time learning base R, trying to implement Shiny effectively will be difficult without the ground knowledge. You current error is because your plot statement is incorrect, it should be `plot(timestamp ~ value, data = loadData())` – mlegge Mar 14 '16 at 18:42