Title: | 'd3.js' Utilities for R |
---|---|
Description: | Provides a suite of functions to help ease the use of 'd3.js' in R. These helpers include 'htmltools::htmlDependency' functions, hierarchy builders, and conversion tools for 'partykit', 'igraph,' 'table', and 'data.frame' R objects into the 'JSON' that 'd3.js' expects. |
Authors: | Mike Bostock [aut, cph] (d3.js library in htmlwidgets/lib, http://d3js.org), Kent Russell [aut, cre, cph] (R interface), Gregor Aisch [aut, cph] (d3-jetpack creator, https://github.com/gka/d3-jetpack), Adam Pearce [aut] (core contributor to d3-jetpack), Ben Ortiz [ctb] |
Maintainer: | Kent Russell <[email protected]> |
License: | BSD_3_clause + file LICENSE |
Version: | 1.1.0 |
Built: | 2024-10-26 02:55:06 UTC |
Source: | https://github.com/timelyportfolio/d3r |
Change Column Name in Children to "name"
change_to_name(x, column = 1)
change_to_name(x, column = 1)
x |
|
column |
column to convert |
data.frame
d3-jetpack is a set of nifty convenience wrappers that speed up
your daily work with d3.js. Must be included after d3_dep_v4()
.
Learn more by reading d3-jetpack
or by watching this YouTube.
d3_dep_jetpack(offline = TRUE)
d3_dep_jetpack(offline = TRUE)
offline |
|
htmltools::htmlDependency
Other 'd3' dependency functions:
d3_dep_v3()
,
d3_dep_v4()
,
d3_dep_v5()
,
d3_dep_v6()
,
d3_dep_v7()
## Not run: library(d3r) library(htmltools) tl <- tagList(tags$script(HTML(sprintf( " var x = 5; var svg = d3.select('body') .append('svg'); svg.append('rect') .at({ x: 100, y: 100, width: 100, height: 100 }) .st({ fill: 'blue', stroke: 'purple' }); " ))), d3_dep_v4(), d3_dep_jetpack()) browsable(tl) tl <- tagList(tags$script(HTML(sprintf( " var svg = d3.select('body') .append('svg'); test_data = [{x: 250, y: 250}, {x: 300, y: 300}, {x: 300, y: 100}]; svg.appendMany(test_data, 'circle') .at({ cx: function(d){return d.x}, cy: function(d){return d.y}, r: 50 }) .st({ fill: 'purple', stroke: 'blue' }); " ))), d3_dep_v4(), d3_dep_jetpack()) browsable(tl) ## End(Not run)
## Not run: library(d3r) library(htmltools) tl <- tagList(tags$script(HTML(sprintf( " var x = 5; var svg = d3.select('body') .append('svg'); svg.append('rect') .at({ x: 100, y: 100, width: 100, height: 100 }) .st({ fill: 'blue', stroke: 'purple' }); " ))), d3_dep_v4(), d3_dep_jetpack()) browsable(tl) tl <- tagList(tags$script(HTML(sprintf( " var svg = d3.select('body') .append('svg'); test_data = [{x: 250, y: 250}, {x: 300, y: 300}, {x: 300, y: 100}]; svg.appendMany(test_data, 'circle') .at({ cx: function(d){return d.x}, cy: function(d){return d.y}, r: 50 }) .st({ fill: 'purple', stroke: 'blue' }); " ))), d3_dep_v4(), d3_dep_jetpack()) browsable(tl) ## End(Not run)
'd3.js' Dependency for Version 3
d3_dep_v3(offline = TRUE)
d3_dep_v3(offline = TRUE)
offline |
|
htmltools::htmlDependency
d3_dep_v7
, d3_dep_v6
, d3_dep_v5
, d3_dep_v4
,
and d3_dep_jetpack
.
Other 'd3' dependency functions:
d3_dep_jetpack()
,
d3_dep_v4()
,
d3_dep_v5()
,
d3_dep_v6()
,
d3_dep_v7()
library(d3r) library(htmltools) tagList(d3_dep_v3())
library(d3r) library(htmltools) tagList(d3_dep_v3())
'd3.js' Dependency for Version 4
d3_dep_v4(offline = TRUE)
d3_dep_v4(offline = TRUE)
offline |
|
htmltools::htmlDependency
d3_dep_v7
, d3_dep_v6
, d3_dep_v5
, d3_dep_v3
,
and d3_dep_jetpack
.
Other 'd3' dependency functions:
d3_dep_jetpack()
,
d3_dep_v3()
,
d3_dep_v5()
,
d3_dep_v6()
,
d3_dep_v7()
library(d3r) library(htmltools) tagList(d3_dep_v4())
library(d3r) library(htmltools) tagList(d3_dep_v4())
'd3.js' Dependency for Version 5
d3_dep_v5(offline = TRUE)
d3_dep_v5(offline = TRUE)
offline |
|
htmltools::htmlDependency
d3_dep_v7
, d3_dep_v6
, d3_dep_v4
, d3_dep_v3
,
and d3_dep_jetpack
.
Other 'd3' dependency functions:
d3_dep_jetpack()
,
d3_dep_v3()
,
d3_dep_v4()
,
d3_dep_v6()
,
d3_dep_v7()
library(d3r) library(htmltools) tagList(d3_dep_v5())
library(d3r) library(htmltools) tagList(d3_dep_v5())
'd3.js' Dependency for Version 6
d3_dep_v6(offline = TRUE)
d3_dep_v6(offline = TRUE)
offline |
|
htmltools::htmlDependency
d3_dep_v7
, d3_dep_v5
, d3_dep_v4
, d3_dep_v3
,
and d3_dep_jetpack
.
Other 'd3' dependency functions:
d3_dep_jetpack()
,
d3_dep_v3()
,
d3_dep_v4()
,
d3_dep_v5()
,
d3_dep_v7()
library(d3r) library(htmltools) tagList(d3_dep_v6())
library(d3r) library(htmltools) tagList(d3_dep_v6())
'd3.js' Dependency for Version 7
d3_dep_v7(offline = TRUE)
d3_dep_v7(offline = TRUE)
offline |
|
htmltools::htmlDependency
d3_dep_v6
, d3_dep_v5
, d3_dep_v4
, d3_dep_v3
,
and d3_dep_jetpack
.
Other 'd3' dependency functions:
d3_dep_jetpack()
,
d3_dep_v3()
,
d3_dep_v4()
,
d3_dep_v5()
,
d3_dep_v6()
library(d3r) library(htmltools) tagList(d3_dep_v7())
library(d3r) library(htmltools) tagList(d3_dep_v7())
Convert 'igraph' to 'd3.js'
d3_igraph(igrf = NULL, json = TRUE)
d3_igraph(igrf = NULL, json = TRUE)
igrf |
|
json |
|
list
## Not run: library(igraph) library(igraphdata) library(d3r) # with random graph d3r::d3_igraph(igraph::sample_pa(100)) # check case where vertices 0 cols d3_igraph(igraph::watts.strogatz.game(1, 50, 4, 0.05)) # with karate from igraphdata # notice graph attributes are added data("karate",package="igraphdata") (karate_d3 <- d3r::d3_igraph(karate)) listviewer::jsonedit(karate_d3) data("kite",package="igraphdata") listviewer::jsonedit(d3_igraph(kite)) ## End(Not run)
## Not run: library(igraph) library(igraphdata) library(d3r) # with random graph d3r::d3_igraph(igraph::sample_pa(100)) # check case where vertices 0 cols d3_igraph(igraph::watts.strogatz.game(1, 50, 4, 0.05)) # with karate from igraphdata # notice graph attributes are added data("karate",package="igraphdata") (karate_d3 <- d3r::d3_igraph(karate)) listviewer::jsonedit(karate_d3) data("kite",package="igraphdata") listviewer::jsonedit(d3_igraph(kite)) ## End(Not run)
Create 'JSON' that 'd3.js' Expects
d3_json(x = NULL, strip = TRUE)
d3_json(x = NULL, strip = TRUE)
x |
data, usually in the form of |
strip |
|
string
of 'JSON' data
data.frame
to a 'd3.js' HierarchyConvert a data.frame
to a 'd3.js' Hierarchy
d3_nest(data = NULL, value_cols = character(), root = "root", json = TRUE)
d3_nest(data = NULL, value_cols = character(), root = "root", json = TRUE)
data |
|
value_cols |
|
root |
|
json |
|
nested data.frame
# convert Titanic to a nested d3 hierarchy # devtools::install_github("timelyportfolio/d3r") library(d3r) library(dplyr) titanic_df <- data.frame(Titanic) tit_tb <- titanic_df %>% select(Class,Age,Survived,Sex,Freq) %>% d3_nest(value_cols="Freq", root="titanic") tit_tb # see as tibble titanic_df %>% select(Class,Age,Survived,Sex,Freq) %>% d3_nest(value_cols="Freq", root="titanic", json=FALSE) # see the structure with listviewer tit_tb %>% listviewer::jsonedit() ## Not run: library(treemap) library(d3r) library(dplyr) library(tidyr) treemap::random.hierarchical.data() %>% d3_nest(value_cols = "x") # use example from ?treemap data(GNI2014) treemap( GNI2014, index=c("continent", "iso3"), vSize="population", vColor="GNI", type="value", draw=FALSE ) %>% {.$tm} %>% select(continent,iso3,color,vSize) %>% d3_nest(value_cols = c("color", "vSize")) ## End(Not run)
# convert Titanic to a nested d3 hierarchy # devtools::install_github("timelyportfolio/d3r") library(d3r) library(dplyr) titanic_df <- data.frame(Titanic) tit_tb <- titanic_df %>% select(Class,Age,Survived,Sex,Freq) %>% d3_nest(value_cols="Freq", root="titanic") tit_tb # see as tibble titanic_df %>% select(Class,Age,Survived,Sex,Freq) %>% d3_nest(value_cols="Freq", root="titanic", json=FALSE) # see the structure with listviewer tit_tb %>% listviewer::jsonedit() ## Not run: library(treemap) library(d3r) library(dplyr) library(tidyr) treemap::random.hierarchical.data() %>% d3_nest(value_cols = "x") # use example from ?treemap data(GNI2014) treemap( GNI2014, index=c("continent", "iso3"), vSize="population", vColor="GNI", type="value", draw=FALSE ) %>% {.$tm} %>% select(continent,iso3,color,vSize) %>% d3_nest(value_cols = c("color", "vSize")) ## End(Not run)
This thing is not even close to being done, so please help with ideas and contributions.
d3_party(tree = NULL, json = TRUE)
d3_party(tree = NULL, json = TRUE)
tree |
|
json |
|
list
or json
depending on json
arg
## Not run: library(d3r) # from ?rpart data("kyphosis", package="rpart") d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) ) # if you want the list instead of json d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis), json = FALSE ) # with ctree instead of rpart # using example from ?ctree d3_party(partykit::ctree(Species ~ .,data = iris)) #devtools::install_github("timelyportfolio/d3treeR") library(d3treeR) d3tree2( d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) ), celltext = "rule", valueField = "n" ) ## End(Not run)
## Not run: library(d3r) # from ?rpart data("kyphosis", package="rpart") d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) ) # if you want the list instead of json d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis), json = FALSE ) # with ctree instead of rpart # using example from ?ctree d3_party(partykit::ctree(Species ~ .,data = iris)) #devtools::install_github("timelyportfolio/d3treeR") library(d3treeR) d3tree2( d3_party( rpart::rpart(Kyphosis ~ Age + Number + Start, data = kyphosis) ), celltext = "rule", valueField = "n" ) ## End(Not run)
Converts Table to 'd3' Nodes and Links
d3_table(tB = NULL, vars = NULL, agg = "Freq")
d3_table(tB = NULL, vars = NULL, agg = "Freq")
tB |
|
vars |
|
agg |
|
list
of two data.frames
- one for nodes and
one for links of the network. This structure is helpful
when working with networkD3
and visNetwork
.
library(d3r) d3_table(Titanic, c("Class","Sex"))
library(d3r) d3_table(Titanic, c("Class","Sex"))
Create V8 Context with D3
d3_v8(...)
d3_v8(...)
... |
arguments passed to |
v8
context with d3.js loaded and available as d3
## Not run: # to do this all in R, please see ggraph # https://github.com/thomasp85/ggraph # by Thomas Lin Pedersen library(d3r) # make a simple data.frame of US state data states <- data.frame( region = as.character(state.region), state = as.character(state.abb), population = state.x77[,"Population"], stringsAsFactors = FALSE ) # use d3_nest to get the data.frame in a d3 hierarchy state_hier <- d3_nest( states, value_cols = "population" ) # use d3_v8 to do something useful with d3 and, our state data ctx <- d3_v8() ctx$eval(sprintf( " var states = %s", state_hier )) ctx$eval( " // we assigned the variable states above // so now make it a real d3 hierarchy var root = d3.hierarchy(states); // sum on population root.sum(function(d) {return d.population ? d.population : 0}); // use d3 to circle pack or state hierarchy d3.pack()(root); // get something we can convert into a data.frame in R var states_packed = []; root.each(function(d) { states_packed.push({ name: d.data.name, radius: d.r, x: d.x, y: d.y }); }); " ) # now get states_packed from our context # to plot in R states_packed <- ctx$get("states_packed") opar <- par(no.readonly=TRUE) # make it square par(pty="s") symbols( states_packed$x, states_packed$y, states_packed$radius, inches=FALSE, asp=1 ) text(y~x, data=states_packed, labels=states_packed$name) # return to original par before we made it square par(opar) # d3.quadtree example library(d3r) x = runif(100) y = runif(100) ctx <- d3_v8() # assign pts as array of pts in V8 ctx$assign("pts", matrix(c(x,y),ncol=2,byrow=TRUE)) # use d3.quadtree() to plot rects ctx$eval( " var d3q = d3.quadtree() .addAll(pts); // nodes function from https://bl.ocks.org/mbostock/4343214 function nodes(quadtree) { var nodes = []; quadtree.visit(function(node, x0, y0, x1, y1) { nodes.push({x0:x0, y0:y0, x1: x1, y1: y1}) }); return nodes; } " ) nodes <- ctx$get("nodes(d3q)", simplifyVector = FALSE) # draw points opar <- par(no.readonly=TRUE) # make it square par(pty="s") plot(y~x) # draw quadtree rects rect( lapply(nodes,function(x){x$x0}), lapply(nodes,function(x){x$y0}), lapply(nodes,function(x){x$x1}), lapply(nodes,function(x){x$y1}) ) par(opar) ## End(Not run)
## Not run: # to do this all in R, please see ggraph # https://github.com/thomasp85/ggraph # by Thomas Lin Pedersen library(d3r) # make a simple data.frame of US state data states <- data.frame( region = as.character(state.region), state = as.character(state.abb), population = state.x77[,"Population"], stringsAsFactors = FALSE ) # use d3_nest to get the data.frame in a d3 hierarchy state_hier <- d3_nest( states, value_cols = "population" ) # use d3_v8 to do something useful with d3 and, our state data ctx <- d3_v8() ctx$eval(sprintf( " var states = %s", state_hier )) ctx$eval( " // we assigned the variable states above // so now make it a real d3 hierarchy var root = d3.hierarchy(states); // sum on population root.sum(function(d) {return d.population ? d.population : 0}); // use d3 to circle pack or state hierarchy d3.pack()(root); // get something we can convert into a data.frame in R var states_packed = []; root.each(function(d) { states_packed.push({ name: d.data.name, radius: d.r, x: d.x, y: d.y }); }); " ) # now get states_packed from our context # to plot in R states_packed <- ctx$get("states_packed") opar <- par(no.readonly=TRUE) # make it square par(pty="s") symbols( states_packed$x, states_packed$y, states_packed$radius, inches=FALSE, asp=1 ) text(y~x, data=states_packed, labels=states_packed$name) # return to original par before we made it square par(opar) # d3.quadtree example library(d3r) x = runif(100) y = runif(100) ctx <- d3_v8() # assign pts as array of pts in V8 ctx$assign("pts", matrix(c(x,y),ncol=2,byrow=TRUE)) # use d3.quadtree() to plot rects ctx$eval( " var d3q = d3.quadtree() .addAll(pts); // nodes function from https://bl.ocks.org/mbostock/4343214 function nodes(quadtree) { var nodes = []; quadtree.visit(function(node, x0, y0, x1, y1) { nodes.push({x0:x0, y0:y0, x1: x1, y1: y1}) }); return nodes; } " ) nodes <- ctx$get("nodes(d3q)", simplifyVector = FALSE) # draw points opar <- par(no.readonly=TRUE) # make it square par(pty="s") plot(y~x) # draw quadtree rects rect( lapply(nodes,function(x){x$x0}), lapply(nodes,function(x){x$y0}), lapply(nodes,function(x){x$x1}), lapply(nodes,function(x){x$y1}) ) par(opar) ## End(Not run)
Apply 'promote_na' to All Rows
promote_na(x)
promote_na(x)
x |
|
data.frame
Promote NA to Top Level
promote_na_one(x)
promote_na_one(x)
x |
|
data.frame