diff --git a/DESCRIPTION b/DESCRIPTION index 8bb87fe..c1827cd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: chorddiag Title: Interactive Chord Diagrams -Version: 0.1.1 +Version: 0.1.2 Authors@R: c( person("Matthias", "Flor", email = "matthias.c.flor@gmail.com", role = c("aut", "cre")), person("Elizabeth", "Tang", email = "eliz.thf@gmail.com", role = "ctb")) diff --git a/R/chorddiag.R b/R/chorddiag.R index 8aba987..27258e0 100644 --- a/R/chorddiag.R +++ b/R/chorddiag.R @@ -68,7 +68,11 @@ #' @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 +#' on a mouse click so that shiny can get the sourceIndex and targetIndex for the +#' purpose of filtering the data on other visualizations +#' @param clickGroupAction character string containing JavaScript code to be executed +#' on a mouse click so that shiny can get the groupIndex +#' #' #' @source Based on \url{http://bl.ocks.org/mbostock/4062006} with several #' modifications. @@ -111,7 +115,8 @@ chorddiag <- function(data, tooltipFontsize = 12, tooltipGroupConnector = " ▶ ", precision = NULL, - clickAction = NULL) { + clickAction = NULL, + clickGroupAction = NULL) { if (!is.matrix(data)) stop("'data' must be a matrix class object.") @@ -214,7 +219,8 @@ chorddiag <- function(data, tooltipUnit = tooltipUnit, tooltipGroupConnector = tooltipGroupConnector, precision = precision, - clickAction = clickAction)) + clickAction = clickAction, + clickGroupAction = clickGroupAction)) params = Filter(Negate(is.null), params) # create widget diff --git a/chorddiag.Rproj b/chorddiag.Rproj index 8ec03ca..d23fd00 100644 --- a/chorddiag.Rproj +++ b/chorddiag.Rproj @@ -9,7 +9,7 @@ UseSpacesForTab: Yes NumSpacesForTab: 4 Encoding: UTF-8 -RnwWeave: Sweave +RnwWeave: knitr LaTeX: pdfLaTeX AutoAppendNewline: Yes diff --git a/inst/examples/shiny/server.R b/inst/examples/shiny/server.R index d61ba4e..84a4c5e 100644 --- a/inst/examples/shiny/server.R +++ b/inst/examples/shiny/server.R @@ -21,10 +21,16 @@ shinyServer(function(input, output) { showTicks = input$showTicks, margin = input$margin, clickAction = "Shiny.onInputChange('sourceIndex', d.source.index+1); - Shiny.onInputChange('targetIndex', d.target.index+1);") + Shiny.onInputChange('targetIndex', d.target.index+1);", + clickGroupAction = "Shiny.onInputChange('groupIndex', d.index+1);") ) - + output$shiny_return <- renderPrint({ - paste0(groupNames[input$sourceIndex], " <-> ", groupNames[input$targetIndex]) + paste0("Clicked chord: ", groupNames[input$sourceIndex], " <-> ", groupNames[input$targetIndex]) + }) + + output$shiny_return2 <- renderPrint({ + paste0("Clicked group: ", groupNames[input$groupIndex]) }) + }) diff --git a/inst/examples/shiny/ui.R b/inst/examples/shiny/ui.R index 5b24e7f..2eab672 100644 --- a/inst/examples/shiny/ui.R +++ b/inst/examples/shiny/ui.R @@ -13,7 +13,8 @@ shinyUI(fluidPage( mainPanel( chorddiagOutput('chorddiag', height = '600px'), - verbatimTextOutput("shiny_return") + verbatimTextOutput("shiny_return"), + verbatimTextOutput("shiny_return2") ) ) )) diff --git a/inst/htmlwidgets/chorddiag.js b/inst/htmlwidgets/chorddiag.js index b710c2c..979c8e2 100644 --- a/inst/htmlwidgets/chorddiag.js +++ b/inst/htmlwidgets/chorddiag.js @@ -60,7 +60,9 @@ HTMLWidgets.widget({ tooltipFontsize = options.tooltipFontsize, tooltipUnit = options.tooltipUnit, tooltipGroupConnector = options.tooltipGroupConnector, - precision = options.precision; + precision = options.precision, + clickAction = options.clickAction, + clickGroupAction = options.clickGroupAction; d3.select(el).selectAll("div.d3-tip").remove(); @@ -155,7 +157,8 @@ HTMLWidgets.widget({ .on("mouseout", function(d) { if (showTooltips) groupTip.hide(d); return groupFade(d, 1); - }); + }) + .on("click", clickGroup); if (groupedgeColor) { groups.style("stroke", groupedgeColor); @@ -346,7 +349,11 @@ HTMLWidgets.widget({ } function click(d) { - return eval(options.clickAction); + return eval(clickAction); + } + + function clickGroup(d) { + return eval(clickGroupAction) } } // end renderValue function diff --git a/man/chorddiag.Rd b/man/chorddiag.Rd index d76c60a..b611572 100644 --- a/man/chorddiag.Rd +++ b/man/chorddiag.Rd @@ -19,7 +19,7 @@ chorddiag(data, type = "directional", width = NULL, height = NULL, showTooltips = TRUE, showZeroTooltips = TRUE, tooltipNames = NULL, tooltipUnit = NULL, tooltipFontsize = 12, tooltipGroupConnector = " ▶ ", precision = NULL, - clickAction = NULL) + clickAction = NULL, clickGroupAction = NULL) } \arguments{ \item{data}{A matrix containing the data. Must be square for the @@ -115,7 +115,11 @@ triangle pointing from source to target.} 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} +on a mouse click so that shiny can get the sourceIndex and targetIndex for the +purpose of filtering the data on other visualizations} + +\item{clickGroupAction}{character string containing JavaScript code to be executed +on a mouse click so that shiny can get the groupIndex} } \description{ Create an interactive chord diagram using the JavaScript visualization