2

Using shiny and rCharts to create a dashboard app, I need to pass raw javascript (not a string) to the Highcharts object.

Given this list

series <- list(data = list(c(0, 0), c(100, 0), c(100, 100)),
               type = 'polygon',
               color = 'Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0.5).get()')

I need to generate this JSON

{series:[{
          data: [[0, 0], [100, 0], [100, 100]],
          type: 'polygon',
          color: Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0.5).get()
        }]
}

but I can't find any way to prevent RJSONIO or jsonlte from quoting the value of the color property

shiny providers the JS() for wrapping literal javascript but RJSONIO ignores it and jsonlite complains about a missing a asJSON method for class JS_EVAL.

Is there some other way to selective prevent quoting of toJSON output?

  • Hi Stephem. Have you tried [highcharter](http://jkunst.com/highcharter/)? Is a wrapper for highcharts based on htmlwidgets so you can pass javascript via `JS()` function. Did you see this question http://stackoverflow.com/questions/27723555 ? The use `"#! ... !#"` to send javascript elements – jbkunst Apr 29 '16 at 01:01
  • Joshua, thanks for letting me know about your package. It's brilliant! – Stephen Zander May 10 '16 at 16:56

2 Answers2

1

You need support from the rCharts author. Two approaches:

  1. The easy approach: use the htmlwidgets framework. Then all you need to do is put your JS code in htmlwidgets::JS(). The JS code will be preserved and evaluated automatically.
  2. The harder approach: reinvent what we have done in htmlwidgets. I don't see why anybody ever wants to do this. Anyway, in case you care about the gory details, what we did were basically:
    1. JS() adds a class JS_EVAL to the string, and we figure out the locations of such strings (e.g. series[2].color);
    2. On the JavaScript side, find and eval() those strings;
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
0

The reason you cannot do this with json libraries is that your code is obviously not JSON but JavaScript. A hack would be to treat the literal string as if it were json:

color <- 'Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0.5).get()'
class(color) <- 'json'
series <- list(data = list(c(0, 0), c(100, 0), c(100, 100)),
   type = 'polygon',
   color = color)

jsonlite::toJSON(series, json_verbatim = TRUE, auto_unbox = TRUE)
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • The package users often do not have access to the serializer `jsonlite::toJSON()` (this is done by the package author internally), so they may not really be able to set `json_verbatim = TRUE`. – Yihui Xie Apr 27 '16 at 22:13
  • @yihui but isn't `json_verbatim = TRUE` the default for shiny and htmlwidgets? – Jeroen Ooms Apr 27 '16 at 22:16
  • It is. The problem is rCharts is not based on htmlwidgets and does not use jsonlite. That is why I asked the OP to seek for help from the rCharts author. – Yihui Xie Apr 28 '16 at 05:13