Skip to content

Instantly share code, notes, and snippets.

@sebkopf
Last active July 27, 2018 01:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebkopf/6955798 to your computer and use it in GitHub Desktop.
Save sebkopf/6955798 to your computer and use it in GitHub Desktop.
library(rCharts)
library(rjson) # required for overwrite
# load rCharts and extend Nvd3 object to allow y2Axis parameters (not implemented in rCharts yet)
nPlot <- function(x, data, ...){
Nvd3mod <- setRefClass('Nvd3mod', contains = 'Nvd3', methods = list(
initialize = function(){
callSuper();
LIB <<- get_lib('nvd3') # library name and url to parent library
templates$script <<- 'NVD3.html' # chart template to include y2Axis-
params <<- c(params, list(y2Axis = list()))
},
y2Axis = function(..., replace = F){
params$y2Axis <<- setSpec(params$y2Axis, ..., replace = replace)
},
getPayload = function(chartId){
# note cannot callSuper() because of the 'toJSON' call that hardcodes the list attributes!
data = toJSONArray(params$data)
chart = toChain(params$chart, 'chart')
xAxis = toChain(params$xAxis, 'chart.xAxis')
x2Axis = toChain(params$x2Axis, 'chart.x2Axis')
yAxis = toChain(params$yAxis, 'chart.yAxis')
y2Axis = toChain(params$y2Axis, 'chart.y2Axis')
opts = toJSON(params[!(names(params) %in% c('data', 'chart', 'xAxis', 'x2Axis', 'yAxis', 'y2Axis'))])
list(opts = opts, xAxis = xAxis, x2Axis = x2Axis, yAxis = yAxis, y2Axis = y2Axis, data = data,
chart = chart, chartId = chartId)
}
))
myChart <- Nvd3mod$new()
myChart$getChartParams(x, data, ...)
return(myChart$copy())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment