rshinyuser-interactionr-dygraphs

Setting the interaction model of a Dygraph in Shiny for R


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.


Solution

  • 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"))