简体   繁体   English

在R Shiny中使用MySQL数据创建时间序列

[英]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. 我正在尝试使用R Shiny开发仪表板,该仪表板从MySQL数据库提取数据并以图形形式表示。 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. 但是,当无法将MySQL中的时间戳值转换为时间序列中的日期时间值时,就会出现问题。 Following is my server.R code: 以下是我的server.R代码:

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. 如果保证时间戳是固定的时间间隔,那么您应该可以沿着ts(result1()[-1], start=myStart, frequency=myFrequency) ,在这里我要删除第一列不想根据时间绘制时间-其他列将作为从myStart开始的时间序列绘制(您应该能够从result1()[1,1]提取该序列(我们不知道该格式为时间戳,因此很难提供指导),而myFrequency将是您每单位时间进行的测量次数。

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. 如果您没有固定的时间点,则使用ts完全是错误的事情-在这种情况下,大多数人似乎都在使用zoo软件包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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