Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions R/sankeyNetwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -74,7 +76,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])
Expand Down Expand Up @@ -130,7 +132,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,
Expand Down
3 changes: 2 additions & 1 deletion inst/examples/shiny/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
7 changes: 6 additions & 1 deletion inst/htmlwidgets/sankeyNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; });
Expand Down Expand Up @@ -242,5 +243,9 @@ HTMLWidgets.widget({
sankey.relayout();
link.attr("d", path);
}

function click(d) {
return eval(options.clickAction);
}
},
});
5 changes: 4 additions & 1 deletion man/sankeyNetwork.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.