diff --git a/R/chordNetwork.R b/R/chordNetwork.R index 971e93fc..74ade7cb 100644 --- a/R/chordNetwork.R +++ b/R/chordNetwork.R @@ -6,7 +6,7 @@ #' \code{NULL} then height is automatically determined based on context) #' @param width numeric width for the network graph's frame area in pixels (if #' \code{NULL} then width is automatically determined based on context) -#' @param initialOpacity specify the opacity before the user mouses over +#' @param initialOpacity specify the opacity before the user mouses over #' the link #' @param colourScale specify the hexadecimal colours in which to display #' the different categories. If there are fewer colours than categories, @@ -21,31 +21,48 @@ #' The default is `0` which means no ticks will be drawn. #' @param labelDistance integer distance in pixels (px) between #' text labels and outer radius. The default is `30`. -#' +#' @param groupLinks Character vector of filenames to hyperlink to when a group +#' arc is clicked. +#' @param groupLinksWindow Where to open a linked page. Defaults to +#' \code{groupLinksWindow = "_self"} and the links are opened in the +#' current browser window. Using \code{groupLinksWindow = "_blank"} +#' would cause a new browser (or tab) to be opened. #' #' @examples #' \dontrun{ -#' #### Data about hair colour preferences, from +#' #### Data about hair colour preferences, from #' ## https://github.com/mbostock/d3/wiki/Chord-Layout -#' +#' #' hairColourData <- matrix(c(11975, 1951, 8010, 1013, #' 5871, 10048, 16145, 990, #' 8916, 2060, 8090, 940, #' 2868, 6171, 8045, 6907), #' nrow = 4) -#' -#' chordNetwork(Data = hairColourData, -#' width = 500, +#' +#' chordNetwork(Data = hairColourData, +#' width = 500, #' height = 500, -#' colourScale = c("#000000", -#' "#FFDD89", -#' "#957244", +#' colourScale = c("#000000", +#' "#FFDD89", +#' "#957244", #' "#F26223"), #' labels = c("red", "brown", "blond", "gray")) -#' +#' +#' +#'chordNetwork(Data = hairColourData, +#' width = 500, +#' height = 500, +#' colourScale = c("#000000", +#' "#FFDD89", +#' "#957244", +#' "#F26223"), +#' labels = c("red", "brown", "blond", "gray"), +#' groupLinks=rep("https://CRAN.R-project.org/package=networkD3", 4), +#' groupLinksWindow="_self") + #' } #' -#' @source +#' @source #' #' Mike Bostock: \url{https://github.com/mbostock/d3/wiki/Chord-Layout}. #' @@ -56,33 +73,35 @@ chordNetwork <- function(Data, width = 500, initialOpacity = 0.8, useTicks = 0, - colourScale = c("#1f77b4", - "#aec7e8", - "#ff7f0e", - "#ffbb78", - "#2ca02c", - "#98df8a", - "#d62728", - "#ff9896", - "#9467bd", - "#c5b0d5", - "#8c564b", - "#c49c94", - "#e377c2", - "#f7b6d2", - "#7f7f7f", - "#c7c7c7", - "#bcbd22", - "#dbdb8d", - "#17becf", + colourScale = c("#1f77b4", + "#aec7e8", + "#ff7f0e", + "#ffbb78", + "#2ca02c", + "#98df8a", + "#d62728", + "#ff9896", + "#9467bd", + "#c5b0d5", + "#8c564b", + "#c49c94", + "#e377c2", + "#f7b6d2", + "#7f7f7f", + "#c7c7c7", + "#bcbd22", + "#dbdb8d", + "#17becf", "#9edae5"), padding = 0.1, fontSize = 14, fontFamily = "sans-serif", labels = c(), - labelDistance = 30) + labelDistance = 30, + groupLinks = FALSE, + groupLinksWindow = "_blank") -{ +{ options <- list( width = width, height = height, @@ -93,14 +112,16 @@ chordNetwork <- function(Data, font_size = fontSize, font_family = fontFamily, labels = labels, - label_distance = labelDistance + label_distance = labelDistance, + group_links = groupLinks, + group_links_window = groupLinksWindow ) - + if (!is.matrix(Data) && !is.data.frame(Data)) { stop("Data must be of type matrix or data frame") } - + if (nrow(Data) != ncol(Data)) { stop(paste("Data must have the same number of rows and columns; given ", @@ -110,16 +131,16 @@ chordNetwork <- function(Data, "columns", sep = " ")) } - + if(length(labels)!=0 && length(labels)!=ncol(Data)){ stop(paste("Length of labels vector should be the same as the number of rows")) } - + if (is.data.frame(Data)) { Data = data.matrix(Data) } - + # create widget htmlwidgets::createWidget( name = "chordNetwork", @@ -138,7 +159,7 @@ chordNetwork <- function(Data, #' @rdname networkD3-shiny #' @export chordNetworkOutput <- function(outputId, width = "100%", height = "500px") { - shinyWidgetOutput(outputId, "chordNetwork", width, height, + shinyWidgetOutput(outputId, "chordNetwork", width, height, package = "networkD3") } diff --git a/inst/htmlwidgets/chordNetwork.js b/inst/htmlwidgets/chordNetwork.js index cbb2dcdc..88001a9f 100755 --- a/inst/htmlwidgets/chordNetwork.js +++ b/inst/htmlwidgets/chordNetwork.js @@ -64,6 +64,20 @@ HTMLWidgets.widget({ .attr("d", d3.arc().innerRadius(innerRadius).outerRadius(outerRadius)) .on("mouseover", fade(.1)) .on("mouseout", fade(1)); + + if(x.options.group_links){ + s.selectAll("g") + .on("click", function(d){ + window.open(x.options.group_links[d.index], name=x.options.group_links_window); + }) + .on("mouseover", function(d){ + d3.select(this).style("cursor", "pointer"); + }) + .on("mouseout", function(d){ + d3.select(this).style("cursor", "default"); + }); + } + if(x.options.labels) { // Forumulas taken from http://sdk.gooddata.com/gooddata-js/example/chord-chart-to-analyze-sales/