rshinyrchartsjsonliterjsonio

Passing unquoted javascript to Highcharts using toJSON


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?


Solution

  • 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;