'd3.js' Utilities for R

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.


CRAN_Status_Badge Travis-CI BuildStatus CoverageStatus

Please see this Building Widgets blog post Why d3r?.

Installing d3r

d3r is on CRAN, so install with install.packages("d3r") or for the absolute latest use devtools.

devtools::install_github("timelyportfolio/d3r")

d3 Dependency Functions

d3r makes d3.js dependency injection in R easy with d3_dep_v3(), d3_dep_v4(), and d3_dep_v5(). These functions work well with htmltools::tags.

library(htmltools)
library(d3r)

# check web developer tools to see d3 is available
browsable(
  attachDependencies(
    tagList(),
    d3_dep_v5()
  )
)

# or include directly in a taglist; I prefer this method.
browsable(
  tagList(
    h1("I have d3 version ", span(id="version")),
    tags$script("d3.select('#version').text(d3.version)"),
    d3_dep_v5()
  )
)

Also, I will commit to keeping d3r up-to-date with d3.js, so you’ll no longer need multiple copies of d3.js for your htmlwidgets. If you are a htmlwidget author, you will no longer need to worry every time d3.js gets a new release. See treebar lines for an example of using d3r with your htmlwidget.

d3 Hierarchy from data.frame

Building d3.js hierarchies can be very difficult. d3r::d3_nest() will convert table and data.frame to a nested d3.js hierarchy ready for work with d3-hierarchy.

d3_nest(as.data.frame(Titanic))

As another example, let’s go from treemap to d3.js.

library(treemap)
library(d3r)

d3_nest(
  treemap::random.hierarchical.data(),
  value_cols = "x"
)

d3 Hierarchy from partykit / rpart

rpart and similar objects in R are very difficult to convert but make perfect subjects for d3 hierarchical layouts. d3_party helps convert these objects for easy usage with d3.js.

#devtools::install_github("timelyportfolio/d3treeR")

library(d3treeR)
library(d3r)

# example from ?rpart
data("kyphosis", package="rpart")
rp <-  rpart::rpart(
  Kyphosis ~ Age + Number + Start,
  data = kyphosis
)

# get the json hierarchy
d3_party(tree=rp)

# interactive plot with d3treeR
d3tree2(
  d3_party(tree=rp),
  celltext="description",
  valueField="n"
)

d3 Network from igraph

igraph to d3.js network of nodes and links is a very common conversion. d3r::d3_igraph will do this for you.

library(igraph)
library(d3r)

d3_igraph(igraph::watts.strogatz.game(1, 50, 4, 0.05))

Todo

I have a whole lot of ideas. Please let me know yours, and let’s make this package great.

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

News

d3r 0.8.4

d3r 0.8.3

  • fix extra slash in offline dependencies

d3r 0.8.2

  • update d3v5 to 5.5.0
  • add httr to Suggests to resolve CRAN issue

d3r 0.8.1

d3r 0.8.0

d3r 0.7.1

d3r 0.7.0

  • update d3v4 to 4.11.0
  • update d3-jetpack to 2.0.9

d3r 0.6.9

Updates

  • modify d3_nest() to work with new tidyr.

d3r 0.6.8

Updates

  • d3_dep_jetpack() added. Essentially the same as d3_dep_v4(), but with a number of convenience functions added. Learn more by checking out the d3-jetpack github page.

  • update d3v4 to 4.10.0

d3r 0.6.7

API Changes

  • (BREAKING) d3_table weight column is now named value to be consistent with the newest d3-sankey plugin commit.

  • Use unpkg.com for online dependencies

d3r 0.6.6

Updates

  • add d3_table() function to convert R table into list of a nodes data.frame and a links data.frame modelled after example

d3r 0.6.5

Updates

d3r 0.6.4

Bug Fix

  • d3_nest would fail with certain hierarchies that had multiple NA level; fix only takes first na_child since non-first children will be duplicates (see commit)

d3r 0.6.3

Updates

  • update d3v4 to 4.8.0
  • test with development dplyr

d3r 0.6.2

Updates

  • update d3v4 to 4.6.0
  • add d3_v8() convenience function to return V8 context with d3.js loaded

d3r 0.6.1

Updates

d3r 0.6.0

Updates

  • add colname to return value (see commit)

  • improve NA child promotion for deeply nested hierarchies

API Changes

  • (BREAKING) change id to name (see issue)

d3r 0.5.0

Updates

API Changes

  • (non-breaking) add offline argument to d3_dep_v3 and d3_dep_v4

d3r 0.4.2

  • CRAN release

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("d3r")

0.8.4 by Kent Russell, 3 months ago


https://github.com/timelyportfolio/d3r


Report a bug at https://github.com/timelyportfolio/d3r/issues


Browse source code at https://github.com/cran/d3r


Authors: Mike Bostock [aut, cph] (d3.js library in htmlwidgets/lib , http://d3js.org) , Kent Russell [aut, cre] (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]


Documentation:   PDF Manual  


BSD_3_clause + file LICENSE license


Imports dplyr, htmltools, tidyr

Suggests httr, jsonlite, listviewer, purrr, testthat

Enhances igraph, partykit, rpart, treemap, V8


Imported by echarts4r, sunburstR.

Suggested by d3Tree.


See at CRAN