简体   繁体   中英

Create Time series with MySQL data in R Shiny

I'm trying to develop a dashboard using R Shiny which extract data from a MySQL database and represents it in graphical form. I need to create a time series using several data. But a problem occurs when the time stamp values in MySQL cannot be converted in to date time values in the time series. Following is my server.R code:

con <- dbConnect(MySQL(),user="root",password="891208",host="localhost",dbname="openPos")
shinyServer(function(input, output) {
 query1 <- reactive({ "SELECT sale_time,sum(quantity_purchased * item_unit_price) 
                            AS revenue, sum(quantity_purchased * item_cost_price) AS cost,
                            sum(quantity_purchased * item_unit_price)-sum(quantity_purchased * item_cost_price) as profit
                            FROM ospos_sales, ospos_sales_items
                            WHERE ospos_sales.sale_id = ospos_sales_items.sale_id
                            GROUP BY sale_time"})
result1 <- reactive({dbGetQuery(con,query1())})
z <- reactive({ts(result1())})
output$ts <- renderPlot({p<-ts.plot(z())
                       print(p)})
output$table <-renderTable({z()})

The output of the table will look like this

Can anyone please tell me why I can't create a time series with these database values?

If the timestamps are guaranteed to be at regular intervals, then you should be fine with something along the lines of ts(result1()[-1], start=myStart, frequency=myFrequency) where I'm removing the first column as you won't want to plot time against time - the other columns will then be plotted as a time series starting from myStart (which you should be able to extract from the result1()[1,1] (we have no idea what format of timestamp you have, so it's hard to give guidance), and myFrequency will be the number of measurements you make per unit of time.

If you don't have regular timepoints, then ts is entirely the wrong thing to use - most people seem to use the zoo package in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM