Skip to content

Commit

Permalink
add clickAction in d3tree2 as point of discussion for #9
Browse files Browse the repository at this point in the history
  • Loading branch information
timelyportfolio committed Aug 16, 2015
1 parent 1f37d1d commit ad0d8b0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions R/d3tree2.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#' default is \code{"name"}.
#' @param valueField \code{character} of the name of the field containing the value on which
#' you would like your treemap based. The default is \code{"size"}.
#' @param clickAction \code{character} or \code{htmlwidgets::JS} to provide additional
#' actions to perform when a tree node/cell is clicked. The JavaScript function should
#' expect one argument, which will be the \code{object} of the node clicked, so the form
#' should look something like \code{function(d){ console.log(d) }}.
#' @param width,height a valid \code{CSS} size for the width and height of the container.
#' Percentage values work also by supplying as \code{character} such as \code{width = "100\%"}
#'
Expand Down Expand Up @@ -74,6 +78,7 @@ d3tree2 <- function(
, celltext = "name"
, id = "id"
, valueField = "size"
, clickAction = NULL
, width = NULL
, height = NULL
) {
Expand Down Expand Up @@ -108,6 +113,11 @@ d3tree2 <- function(
# accept list
# here we shouldn't need to do anything

# convert clickAction if character to htmlwidgets::JS
if(is.character(clickAction) && !inherits(clickAction,"JS_EVAL")){
clickAction <- htmlwidgets::JS(clickAction)
}

# forward options using x
x = list(
data = data
Expand All @@ -117,6 +127,7 @@ d3tree2 <- function(
celltext = celltext
,id = id
,valueField = valueField
,clickAction = clickAction
)
)

Expand Down
5 changes: 5 additions & 0 deletions inst/htmlwidgets/d3tree2.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ HTMLWidgets.widget({
Shiny.onInputChange(el.id + '_click', {name:d[celltext]})

This comment has been minimized.

Copy link
@gahoo

gahoo Aug 31, 2015

Since sometimes 'celltext' might not the same as 'id', wouldn't it better to return 'd' instead? Is there other way to get 'id' when 'celltext' is 'name' which is not unique. Note, celltext is an argument to d3tree2, so you could specify a unique value, but I understand why this could be a suboptimal solution.

This comment has been minimized.

Copy link
@timelyportfolio

timelyportfolio Aug 31, 2015

Author Collaborator

d is circular, so does not readily work. However, there is a solution. I'll try to implement, so all non-circular top-level information is included.

This comment has been minimized.

Copy link
@gahoo

gahoo Sep 2, 2015

How about add key to the end to specify which data should be included?

Shiny.onInputChange(el.id + '_click_' + key, {value:d[key]})
}

// check for additional clickAction to perform
if( x.options.clickAction && typeof x.options.clickAction === "function" ){
x.options.clickAction(d);
}

return g;
}

Expand Down
7 changes: 6 additions & 1 deletion man/d3tree2.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\title{Create an Interactive Treemap (Version 2)}
\usage{
d3tree2(data = NULL, rootname = NULL, celltext = "name", id = "id",
valueField = "size", width = NULL, height = NULL)
valueField = "size", clickAction = NULL, width = NULL, height = NULL)
}
\arguments{
\item{data}{the data to be plotted as either
Expand All @@ -26,6 +26,11 @@ default is \code{"name"}.}
\item{valueField}{\code{character} of the name of the field containing the value on which
you would like your treemap based. The default is \code{"size"}.}

\item{clickAction}{\code{character} or \code{htmlwidgets::JS} to provide additional
actions to perform when a tree node/cell is clicked. The JavaScript function should
expect one argument, which will be the \code{object} of the node clicked, so the form
should look something like \code{function(d){ console.log(d) }}.}

\item{width,height}{a valid \code{CSS} size for the width and height of the container.
Percentage values work also by supplying as \code{character} such as \code{width = "100\%"}}
}
Expand Down

0 comments on commit ad0d8b0

Please sign in to comment.