hrbrmstr / taucharts

:bar_chart: An R htmlwidget interface to the TauCharts javascript library
http://rpubs.com/hrbrmstr/taucharts
Other
65 stars 10 forks source link

add handling for xts objects and crude date conversion #10

Closed timelyportfolio closed 9 years ago

timelyportfolio commented 9 years ago

This is very much up for discussion. I borrowed some code from dygraphs to convert Date/POSIXct to ISO8601 format, which tauCharts accepts. In addition, if data is xts then it will convert to a data.frame with a column Date for the index. I have not tested this on all the various complicated date types in R, but at least it is a start.

Question: is a dependency on xts ok since this will only be useful for a subset of folks? We could instead still handle the date conversion but require a xts user to convert to an appropriate data.frame.

library(taucharts)
library(quantmod)

spy <- getSymbols("SPY",auto.assign=F)

tauchart( spy ) %>% tau_line( "Date", "SPY.Close" )

# test with as.POSIXct
index(spy) <- as.POSIXct(index(spy))
tauchart(spy) %>% tau_line("Date","SPY.Close") %>% tau_guide_x( tick_format = "%Y")

data.frame(
  date = seq.Date( as.Date("2000-01-01"), as.Date("2000-12-31"), by = "month" )
  ,value = runif(12,0,100)
) %>%
  tauchart() %>%
  tau_point("date","value") %>%
  tau_guide_x( tick_format = "%b %y" )
timelyportfolio commented 9 years ago

As this progresses, we can probably leverage more deeply all the work done with time by @jjallaire with dygraphs.

hrbrmstr commented 9 years ago

xts only has a dependency on zoo, so it's not exactly a heavyweight dependency (I mean, ggplot2 depends on plyr which is super-heavyweight since it depends on Rcpp).

hrbrmstr commented 9 years ago

For:

index(spy) <- as.POSIXct(index(spy))
tauchart(spy) %>% tau_point("Date","SPY.Close") %>% tau_guide_x( tick_format = "%Y")

I'm getting:

Error in as.POSIXct.default(x, tz = "GMT") : do not know how to convert 'x' to class “POSIXct”

timelyportfolio commented 9 years ago

Actually, I think I can avoid the dependency on xts entirely. Let me rework the code a bit. See #11

timelyportfolio commented 9 years ago

I'm not sure on the as.POSIXct. Is class(index(spy)) == "Date" before attempting the conversion? It is not really important. I was just showing that it handles both Date and POSIXct.