/
d2b.R
198 lines (188 loc) · 6.38 KB
/
d2b.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#' Sunburst Using 'd2b'
#'
#' Create interactive sunburst chart with the 'd2b' charting library.
#'
#' @param data data in csv source,target form or in
#' nested d3 JSON hierarchy with `{name:..., children:[];}`. \code{list}, \code{character},
#' or \code{connection} data will be assumed to be \code{JSON}.
#' \code{data.frame} data will be assumed to be \code{csvdata} and converted
#' to \code{JSON} by \code{sunburstR:::csv_to_hier()}.
#' @param colors \code{vector} of strings representing colors as hexadecimal for
#' manual colors. If you want precise control of colors, supply a \code{list}
#' with \code{range} and/or \code{domain}. For advanced customization, supply
#' a JavaScript \code{function}.
#' @param valueField \code{character} for the field to use to calculate size. The default
#' value is \code{"size"}.
#' @param tooltip \code{list} of options for customizing the tooltip. See the helper
#' function \code{\link{sund2bTooltip}} for more information.
#' @param breadcrumbs \code{list} of options for customizing the breadcrumb. See the helper
#' function \code{\link{sund2bBreadcrumb}} for more information.
#' @param rootLabel \code{character} to label root node something other than 'root'.
#' @param showLabels \code{logical} to show labels on the slices. The default is \code{FALSE}.
#' @param height,width height and width of sunburst htmlwidget containing div
#' specified in any valid \code{CSS} size unit.
#' @param elementId string id as a valid \code{CSS} element id.
#'
#' @example inst/examples/example_sund2b.R
#'
#' @import htmlwidgets
#'
#' @export
sund2b <- function(
data = NULL,
colors = NULL,
valueField = "size",
tooltip = NULL,
breadcrumbs = NULL,
rootLabel = NULL,
showLabels = FALSE,
width = NULL, height = NULL, elementId = NULL
) {
if(is.null(data)) stop("please provide data",call.=FALSE)
# accept JSON string as data
if( inherits(data,c("character","connection")) ){
data = jsonlite::toJSON(
jsonlite::fromJSON( data )
, auto_unbox = TRUE
, dataframe = "rows"
)
}
# accept list as data
if( inherits(data, "list") ) {
data = jsonlite::toJSON(
data
, auto_unbox = TRUE
, dataframe = "rows"
)
}
# accept data.frame as data
# and convert to JSON with csv_to_hier
# this conversion should be more robust than
# the JavaScript converter
if( inherits(data, "data.frame") ) {
data = csv_to_hier(data)
}
# forward options using x
x = list(
data = list(
root = data,
tooltip = Filter(Negate(is.null), tooltip),
breadcrumbs = Filter(Negate(is.null), breadcrumbs)
),
options = list(
colors = colors,
valueField = valueField,
rootLabel = rootLabel,
showLabels = showLabels
)
)
# create widget
htmlwidgets::createWidget(
name = 'sund2b',
x,
width = width,
height = height,
package = 'sunburstR',
elementId = elementId,
dependencies = list(
d3r::d3_dep_v4(),
d2b_dep()
)
)
}
#' Advanced Customization of 'd2b' Tooltip
#'
#' @param at \code{character} which should be one of
#' "top left", "top center", "top right", "center left", "center center",
#' "center right", "bottom center", "bottom right" to specify
#' where the tooltip will be positioned relative to the hovered item.
#' @param followMouse \code{logical} controlling whether the tooltip
#' will follow the mouse instead of being placed in a static
#' position relative to the hovered element
#' @param html \code{character} or \code{htmlwidgets::JS} to customize the content
#' of the tooltip. To provide a function, the arguments for the 'JavaScript'
#' function will be 'function(nodedata, size, percent)' and the function
#' should return a string.
#' @param my \code{character} which should be one of "top", "left", "right", "bottom"
#' to control the orientation of the tooltip.
#'
#' @return \code{list}
#' @export
#'
#' @example inst/examples/example_sund2bTooltip.R
sund2bTooltip <- function(
at = NULL,
followMouse = NULL,
html = NULL,
my = NULL
) {
list(
at = at,
followMouse = followMouse,
html = html,
my = my
)
}
#' Advanced Customization of 'd2b' Breadcrumb
#'
#' @param enabled \code{boolean} to enable or disable the breadcrumbs.
#' @param html \code{character} or \code{htmlwidgets::JS} to customize the content
#' of the breadcrumb. To provide a function, the arguments for the 'JavaScript'
#' function will be 'function(nodedata, size, percent)' and the function
#' should return a string.
#' @param orient \code{character} which should be one of "top", "left", "right", "bottom"
#' to control the orientation of the breadcrumb relative to the chart.
#'
#' @return \code{list}
#' @export
#'
#' @example inst/examples/example_sund2bBreadcrumb.R
sund2bBreadcrumb <- function(
enabled = NULL,
html = NULL,
orient = NULL
) {
list(
enabled = enabled,
html = html,
orient = orient
)
}
#' Shiny bindings for d2b
#'
#' Output and render functions for using d2b within Shiny
#' applications and interactive Rmd documents.
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#' \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#' string and have \code{'px'} appended.
#' @param expr An expression that generates a d2b
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
#' is useful if you want to save an expression in a variable.
#'
#' @name d2b-shiny
#'
#' @export
sund2bOutput <- function(outputId, width = '100%', height = '400px'){
htmlwidgets::shinyWidgetOutput(outputId, 'sund2b', width, height, package = 'sunburstR')
}
#' @rdname d2b-shiny
#' @export
renderSund2b <- function(expr, env = parent.frame(), quoted = FALSE) {
if (!quoted) { expr <- substitute(expr) } # force quoted
htmlwidgets::shinyRenderWidget(expr, sund2bOutput, env, quoted = TRUE)
}
#' @keywords internal
d2b_dep <- function() {
htmltools::htmlDependency(
name = "d2b",
version = "1.0.9",
src = c(
file = system.file("htmlwidgets/lib/d2b", package="sunburstR")
),
script = "d2b.min.js",
stylesheet = "d2b_custom.css"
)
}