Skip to content

Commit

Permalink
Add clickGroupAction option.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattflor committed Jul 24, 2017
1 parent e26b609 commit 82975eb
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion 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"))
Expand Down
12 changes: 9 additions & 3 deletions R/chorddiag.R
Expand Up @@ -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.
Expand Down Expand Up @@ -111,7 +115,8 @@ chorddiag <- function(data,
tooltipFontsize = 12,
tooltipGroupConnector = " &#x25B6; ",
precision = NULL,
clickAction = NULL) {
clickAction = NULL,
clickGroupAction = NULL) {

if (!is.matrix(data))
stop("'data' must be a matrix class object.")
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion chorddiag.Rproj
Expand Up @@ -9,7 +9,7 @@ UseSpacesForTab: Yes
NumSpacesForTab: 4
Encoding: UTF-8

RnwWeave: Sweave
RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
Expand Down
12 changes: 9 additions & 3 deletions inst/examples/shiny/server.R
Expand Up @@ -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])
})

})
3 changes: 2 additions & 1 deletion inst/examples/shiny/ui.R
Expand Up @@ -13,7 +13,8 @@ shinyUI(fluidPage(

mainPanel(
chorddiagOutput('chorddiag', height = '600px'),
verbatimTextOutput("shiny_return")
verbatimTextOutput("shiny_return"),
verbatimTextOutput("shiny_return2")
)
)
))
13 changes: 10 additions & 3 deletions inst/htmlwidgets/chorddiag.js
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions man/chorddiag.Rd

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

0 comments on commit 82975eb

Please sign in to comment.