Stef Mientki
2016-Sep-05 20:28 UTC
[R] how to transform db query result into a set of timeseries ?
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
Joshua Ulrich
2016-Sep-07 23:56 UTC
[R] how to transform db query result into a set of timeseries ?
There's no reason to create multiple xts objects if you intend to merge them all into one object. You say the user can select timeseries, so I assume you have column names in a vector. If so, you can do something like this: series <- c("M", "G", "N") ... zonnen <- xts(df[,series], as.POSIXct(df$Date)) dygraph(zonnen) On Mon, Sep 5, 2016 at 3:28 PM, Stef Mientki <stef.mientki at gmail.com> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com