0

I am trying to recreate an rCharts-based, interactive dygraphs chart by running the code.R from this timelyportfolio/rCharts_dygraphs GitHub repository.

Compared to what it should look like, my chart however doesn't render completely:

By comparing html page sources, I think the problem is that the date array hasn't evaluated properly:

"date": [ "#! new Date(252374400000)!#", "#! new Date(283910400000)!#", .... ]

Corresponding R source code snippet contains #! annotation unfamiliar to me:

fama.df$date <- paste0(
  "#! new Date(",
  as.numeric(as.POSIXct(paste0(fama.df$year,"-12-31"))) * 1000,
  ")!#"
)

What is the background for this annotation and how can I fix this?

Daniel Krizian
  • 4,586
  • 4
  • 38
  • 75
  • 1
    The background for the `#!... !#` annotation is to tag an object as a javascript literal so that it is not transformed into a string during conversion to JSON. Can you post the entire code that you were trying to evaluate so that it is clearer to me what exactly you were trying? – Ramnath Apr 04 '14 at 00:39
  • Thanks @Ramnath , the entire code is in the first link of the post. That repository contains all the relevant files, I've just copy-pasted and run entire `code.R`, while having `chart.html`, `config.yml`, `dygraph-combined.js` and `lodash.js` copied in the same working directory. – Daniel Krizian Apr 04 '14 at 09:07
  • @Ramnath, I've just noticed [in these lines of `rCharts` source](https://github.com/ramnathv/rCharts/blob/e917d6e93334b111e9cdbe3d5607636ab5832161/R/toJSON.R#L106-L108) that `toObj` function has been commented out and replaced. Now note the difference: `x <- paste0("#! new Date(", as.numeric(as.POSIXct(paste0(2011,"-12-31"))) * 1000, ")!#"); gsub('\"#!(.*?)!#\"', "\\1", x); gsub('#!(.*)!#', "\\1", x)`. Is this source edit the cause? @timelyportfolio published the original code in autumn 2013. – Daniel Krizian Apr 04 '14 at 09:41
  • It is def related to that piece of code, but I am not sure why it broke. Can you file this as an issue on github> – Ramnath Apr 04 '14 at 13:25
  • @Ramnath, filed as https://github.com/ramnathv/rCharts/issues/394 – Daniel Krizian Apr 04 '14 at 16:10

1 Answers1

1

I believe this was a newer implementation of rCharts. Try installing from that branch, but after running be sure to get current again. I was playing around with how to handle dates like googleVis.

require(devtools)
install_github("rCharts","timelyportfolio",ref="dimple_layer")

Also, you will need to do like in the updated code.R

dy1$setTemplate(afterScript = "<script></script>"

You might notice that dygraphs is not fully developed. We are starting an rChartsExtra to house the experiments/alternate libraries.

timelyportfolio
  • 6,479
  • 30
  • 33
  • thank you, worked. I am very much grateful for the pioneering integration of the `R` and `js` worlds in your and @Ramnath repositories - great learning material – Daniel Krizian Apr 05 '14 at 23:10