From 1f54e9bef642a8a6fcf974f8d2d3cfbc0eda6ba7 Mon Sep 17 00:00:00 2001 From: eliztang Date: Thu, 14 Jul 2016 19:12:53 +0800 Subject: [PATCH 1/4] added clickAction --- R/sankeyNetwork.R | 4 ++-- inst/examples/shiny/server.R | 3 ++- inst/htmlwidgets/sankeyNetwork.js | 7 ++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index b5214674..2e828728 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -74,7 +74,7 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = "", colourScale = JS("d3.scale.category20()"), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, margin = NULL, - height = NULL, width = NULL, iterations = 32) + height = NULL, width = NULL, iterations = 32, clickAction = NULL) { # Check if data is zero indexed check_zero(Links[, Source], Links[, Target]) @@ -130,7 +130,7 @@ sankeyNetwork <- function(Links, Nodes, Source, Target, Value, options = list(NodeID = NodeID, NodeGroup = NodeGroup, LinkGroup = LinkGroup, colourScale = colourScale, fontSize = fontSize, fontFamily = fontFamily, nodeWidth = nodeWidth, nodePadding = nodePadding, units = units, - margin = margin, iterations = iterations) + margin = margin, iterations = iterations, clickAction = clickAction) # create widget htmlwidgets::createWidget(name = "sankeyNetwork", x = list(links = LinksDF, diff --git a/inst/examples/shiny/server.R b/inst/examples/shiny/server.R index c88370b3..efe6d336 100644 --- a/inst/examples/shiny/server.R +++ b/inst/examples/shiny/server.R @@ -31,7 +31,8 @@ shinyServer(function(input, output) { Energy <- jsonlite::fromJSON(URL) sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", - fontSize = 12, nodeWidth = 30) + fontSize = 12, nodeWidth = 30, + clickAction="alert(d.source.name+' <-> '+d.target.name);") }) output$rt <- renderRadialNetwork({ diff --git a/inst/htmlwidgets/sankeyNetwork.js b/inst/htmlwidgets/sankeyNetwork.js index 2e9321dd..3a9abcf5 100644 --- a/inst/htmlwidgets/sankeyNetwork.js +++ b/inst/htmlwidgets/sankeyNetwork.js @@ -137,7 +137,8 @@ HTMLWidgets.widget({ .on("mouseout", function(d) { d3.select(this) .style("stroke-opacity", opacity_link); - }); + }) + .on("click", click); // add backwards class to cycles link.classed('backwards', function (d) { return d.target.x < d.source.x; }); @@ -242,5 +243,9 @@ HTMLWidgets.widget({ sankey.relayout(); link.attr("d", path); } + + function click(d) { + return eval(options.clickAction); + } }, }); From a79a7ba75a8f2a293cd042acefb7bcba77108a1e Mon Sep 17 00:00:00 2001 From: eliztang Date: Thu, 14 Jul 2016 19:24:31 +0800 Subject: [PATCH 2/4] correction cos uni-directional --- inst/examples/shiny/server.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/examples/shiny/server.R b/inst/examples/shiny/server.R index efe6d336..788a2a84 100644 --- a/inst/examples/shiny/server.R +++ b/inst/examples/shiny/server.R @@ -32,7 +32,7 @@ shinyServer(function(input, output) { sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source", Target = "target", Value = "value", NodeID = "name", fontSize = 12, nodeWidth = 30, - clickAction="alert(d.source.name+' <-> '+d.target.name);") + clickAction="alert(d.source.name+' -> '+d.target.name);") }) output$rt <- renderRadialNetwork({ From c655271ecab55a87c6cba0fec081aaf9543c372f Mon Sep 17 00:00:00 2001 From: eliztang Date: Thu, 14 Jul 2016 21:02:39 +0800 Subject: [PATCH 3/4] added documentation on clickAction --- R/sankeyNetwork.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 2e828728..053ac85f 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -39,6 +39,8 @@ #' @param iterations numeric. Number of iterations in the diagramm layout for #' computation of the depth (y-position) of each node. Note: this runs in the #' browser on the client so don't push it too high. +#' @param clickAction character string with a JavaScript expression to evaluate +#' when a link is clicked. #' #' @examples #' \dontrun{ From d8dbd3711fa1e8943f70d6e647b01180638e6ece Mon Sep 17 00:00:00 2001 From: eliztang Date: Thu, 14 Jul 2016 21:02:39 +0800 Subject: [PATCH 4/4] added documentation on clickAction --- R/sankeyNetwork.R | 2 ++ man/sankeyNetwork.Rd | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/R/sankeyNetwork.R b/R/sankeyNetwork.R index 2e828728..053ac85f 100644 --- a/R/sankeyNetwork.R +++ b/R/sankeyNetwork.R @@ -39,6 +39,8 @@ #' @param iterations numeric. Number of iterations in the diagramm layout for #' computation of the depth (y-position) of each node. Note: this runs in the #' browser on the client so don't push it too high. +#' @param clickAction character string with a JavaScript expression to evaluate +#' when a link is clicked. #' #' @examples #' \dontrun{ diff --git a/man/sankeyNetwork.Rd b/man/sankeyNetwork.Rd index b35e188c..7bf5a2b6 100644 --- a/man/sankeyNetwork.Rd +++ b/man/sankeyNetwork.Rd @@ -11,7 +11,7 @@ specifically for Sankey diagrams \url{http://bost.ocks.org/mike/sankey/}. sankeyNetwork(Links, Nodes, Source, Target, Value, NodeID, NodeGroup = NodeID, LinkGroup = NULL, units = "", colourScale = JS("d3.scale.category20()"), fontSize = 7, fontFamily = NULL, nodeWidth = 15, nodePadding = 10, - margin = NULL, height = NULL, width = NULL, iterations = 32) + margin = NULL, height = NULL, width = NULL, iterations = 32, clickAction = NULL) } \arguments{ \item{Links}{a data frame object with the links between the nodes. It should @@ -70,6 +70,9 @@ to accomodate long text labels.} \item{iterations}{numeric. Number of iterations in the diagramm layout for computation of the depth (y-position) of each node. Note: this runs in the browser on the client so don't push it too high.} + +\item{clickAction}{character string with a JavaScript expression to evaluate +when a node is clicked.} } \description{ Create a D3 JavaScript Sankey diagram