diff --git a/R/chorddiag.R b/R/chorddiag.R index a1da5ec..3b951bb 100644 --- a/R/chorddiag.R +++ b/R/chorddiag.R @@ -65,6 +65,7 @@ #' triangle pointing from source to target. #' @param precision Integer number of significant digits to be used for tooltip #' display. +#' @param clickAction character string containing JavaScript code to be executed on a mouse click so that shiny can get the sourceIndex and targetIndex for the purpose of filtering the data on other visualizations #' #' @source Based on \url{http://bl.ocks.org/mbostock/4062006} with several #' modifications. @@ -105,7 +106,8 @@ chorddiag <- function(data, tooltipUnit = NULL, tooltipFontsize = 12, tooltipGroupConnector = " ▶ ", - precision = NULL) { + precision = NULL, + clickAction = NULL) { if (!is.matrix(data)) stop("'data' must be a matrix class object.") @@ -206,7 +208,8 @@ chorddiag <- function(data, tooltipFontsize = tooltipFontsize, tooltipUnit = tooltipUnit, tooltipGroupConnector = tooltipGroupConnector, - precision = precision)) + precision = precision, + clickAction = clickAction)) params = Filter(Negate(is.null), params) # create widget diff --git a/inst/examples/shiny/server.R b/inst/examples/shiny/server.R index 7c22112..d61ba4e 100644 --- a/inst/examples/shiny/server.R +++ b/inst/examples/shiny/server.R @@ -19,6 +19,12 @@ shinyServer(function(input, output) { groupColors = groupColors, groupnamePadding = input$groupnamePadding, showTicks = input$showTicks, - margin = input$margin) + margin = input$margin, + clickAction = "Shiny.onInputChange('sourceIndex', d.source.index+1); + Shiny.onInputChange('targetIndex', d.target.index+1);") ) + + output$shiny_return <- renderPrint({ + paste0(groupNames[input$sourceIndex], " <-> ", groupNames[input$targetIndex]) + }) }) diff --git a/inst/examples/shiny/ui.R b/inst/examples/shiny/ui.R index bd742a2..5b24e7f 100644 --- a/inst/examples/shiny/ui.R +++ b/inst/examples/shiny/ui.R @@ -12,7 +12,8 @@ shinyUI(fluidPage( ), mainPanel( - chorddiagOutput('chorddiag', height = '600px') + chorddiagOutput('chorddiag', height = '600px'), + verbatimTextOutput("shiny_return") ) ) )) diff --git a/inst/htmlwidgets/chorddiag.js b/inst/htmlwidgets/chorddiag.js index 811c7a2..136c7d3 100644 --- a/inst/htmlwidgets/chorddiag.js +++ b/inst/htmlwidgets/chorddiag.js @@ -217,7 +217,8 @@ HTMLWidgets.widget({ .on("mouseout", function(d) { if (showTooltips) chordTip.hide(d); return chordFade(d, 1); - }); + }) + .on("click", click); // create group labels if (showGroupnames) { @@ -338,6 +339,10 @@ HTMLWidgets.widget({ var mult = Math.pow(10, sig - Math.floor(Math.log(n) / Math.LN10) - 1); return Math.round(n * mult) / mult; } + + function click(d) { + return eval(options.clickAction); + } } // end renderValue function diff --git a/man/chorddiag.Rd b/man/chorddiag.Rd index 73a03db..05b9377 100644 --- a/man/chorddiag.Rd +++ b/man/chorddiag.Rd @@ -18,7 +18,7 @@ chorddiag(data, type = "directional", width = NULL, height = NULL, tickInterval = NULL, ticklabelFontsize = 10, fadeLevel = 0.1, showTooltips = TRUE, tooltipNames = NULL, tooltipUnit = NULL, tooltipFontsize = 12, tooltipGroupConnector = " ▶ ", - precision = NULL) + precision = NULL, clickAction = NULL) } \arguments{ \item{data}{A matrix containing the data. Must be square for the @@ -109,6 +109,8 @@ triangle pointing from source to target.} \item{precision}{Integer number of significant digits to be used for tooltip display.} + +\item{clickAction}{character string containing JavaScript code to be executed on a mouse click so that shiny can get the sourceIndex and targetIndex for the purpose of filtering the data on other visualizations} } \description{ Create an interactive chord diagram using the JavaScript visualization