问题
I am looking to add the custom interaction seen at http://dygraphs.com/gallery/#g/interaction under "Custom interaction model" into my Shiny web app.
As far as I understand it, this requires attaching some JS to the page and setting the interaction model on the graph:
interactionModel : {
'mousedown' : downV3,
'mousemove' : moveV3,
'mouseup' : upV3,
'click' : clickV3,
'dblclick' : dblClickV3,
'mousewheel' : scrollV3
}
However, interactionModel
does not seem to be listed as a parameter in the dyOptions
function on the R side.
Is there a way to work around this?
Update:
Looking at the source for dyOptions
, it seems that options can be modified directly:
g <- dyGraph(series)
g$x$attr$option <- "Value"
However, setting the interactionModel
here does not seem to work.
See: https://github.com/rstudio/dygraphs/blob/master/R/options.R
Update:
You can indeed set the options using:
g$x$attrs$option <- "Value" # Note that it is "attrs", not "attr"
This can be used to switch off the interaction mode:
graph$x$attrs$interactionModel <- "{}"
The remaining problem is passing JS function references via JSON to the page.
回答1:
You can use the JS function to pass JavaScript over JSON to the client.
In ui.R:
tags$head(tags$script(src="interaction.js"))
In server.R:
g <- dygraph(series(), main = "Graph", xlab = "Date", ylab = "Amount") %>%
dySeries(label = "X")
g$x$attrs$interactionModel <- list(
mousedown = JS("downV3"),
mousemove = JS("moveV3"),
mouseup = JS("upV3"),
click = JS("clickV3"),
dblclick = JS("dblClickV3"),
mousewheel = JS("scrollV3"))
来源:https://stackoverflow.com/questions/29098290/setting-the-interaction-model-of-a-dygraph-in-shiny-for-r