[R] how to transform db query result into a set of timeseries ?

Stef Mientki stef.mientki at gmail.com
Mon Sep 5 22:28:50 CEST 2016


hello,

I've a number of timeseries into a database and want to display these 
timeseries into graph.

Now the code below works well, but as the user can select which 
timeseries should be shown (up to 20 timeseries) the code below should 
be dynamic and can be quiet large and complex.

Is there an easier way to convert a database result into timeseries 
accepted by dygraph ?

     SQL <- "select Date, M, G, N from Compare_Model"
     df <- dbGetQuery ( con, statement = SQL )

     zon1 <- xts ( df$M,  as.POSIXct ( df$Date, format="%Y-%m-%d 
%H:%M:%S") )
     zon2 <- xts ( df$G,  as.POSIXct ( df$Date, format="%Y-%m-%d 
%H:%M:%S") )
     zon3 <- xts ( df$N,  as.POSIXct ( df$Date, format="%Y-%m-%d 
%H:%M:%S") )

     zonnen <- Reduce ( function(...) merge(..., all=TRUE ), list ( zon, 
zon2, zon3 ))

     dygraph ( zonnen )


thanks,

Stef



More information about the R-help mailing list