• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

R/H28-May-2018-

docs/H03-May-2022-

inst/H28-May-2018-

man/H28-May-2018-

pkgdown/H03-May-2022-

tests/H28-May-2018-

tools/H28-May-2018-

vignettes/H28-May-2018-

.RbuildignoreH A D28-May-2018432

.gitignoreH A D28-May-2018101

.travis.ymlH A D28-May-20181.2 KiB

DESCRIPTIONH A D28-May-20181.2 KiB

LICENSEH A D28-May-201868

LICENSE.mdH A D28-May-20181.5 KiB

NAMESPACEH A D28-May-2018447

NEWS.mdH A D28-May-2018344

README.mdH A D28-May-201810.6 KiB

r2d3.RprojH A D28-May-2018343

README.md

1r2d3: R Interface to D3 Visualizations
2================
3
4<a href="https://travis-ci.org/rstudio/r2d3"><img src="https://travis-ci.org/rstudio/r2d3.svg?branch=master" style="border: none; margin: 0px; margin-right: 5px;"/></a>
5<a href="https://cran.r-project.org/package=r2d3"><img src="https://www.r-pkg.org/badges/version/r2d3" style="border: none; margin: 0px; margin-right: 5px;"/></a>
6<a href="https://codecov.io/gh/rstudio/r2d3"><img src="https://codecov.io/gh/rstudio/r2d3/branch/master/graph/badge.svg" style="border: none; margin: 0px; margin-right: 5px;"/></a>
7
8<img src="tools/README/r2d3-hex.png" width=180 align="right" style="border: none; margin-right: 10px;"/>
9
10The **r2d3** package provides a suite of tools for using [D3
11visualizations](https://d3js.org/) with R, including:
12
13  - Translating R objects into D3 friendly data structures
14
15  - Rendering D3 scripts within the [RStudio
16    Viewer](https://support.rstudio.com/hc/en-us/articles/202133558-Extending-RStudio-with-the-Viewer-Pane)
17    and [R Notebooks](https://rmarkdown.rstudio.com/r_notebooks.html)
18
19  - Publishing D3 visualizations to the web
20
21  - Incorporating D3 scripts into [R
22    Markdown](https://rmarkdown.rstudio.com/) reports, presentations,
23    and dashboards
24
25  - Creating interactive D3 applications with
26    [Shiny](https://shiny.rstudio.com/)
27
28  - Distributing D3 based [htmlwidgets](http://www.htmlwidgets.org) in R
29    packages
30
31<div style="clear: both">
32
33</div>
34
35<br/>
36
37With **r2d3**, you can bind data from R to D3 visualizations like the
38ones found on <https://github.com/d3/d3/wiki/Gallery>,
39<https://bl.ocks.org/>, and
40<https://vida.io/explore>:
41
42<div style="margin-top: 20px; margin-bottom: 10px;">
43
44<a href="https://rstudio.github.io/r2d3/articles/gallery/chord/"><img src="tools/README/chord_thumbnail.png" width="28%" class="illustration gallery-thumbnail"/></a>  <a href="https://rstudio.github.io/r2d3/articles/gallery/bubbles/"><img src="tools/README/bubbles_thumbnail.png" width="28%" class="illustration gallery-thumbnail"/></a>  <a href="https://rstudio.github.io/r2d3/articles/gallery/cartogram/"><img src="tools/README/cartogram_thumbnail.png" width="28%" class="illustration gallery-thumbnail"/></a>
45
46</div>
47
48D3 visualizations created with **r2d3** work just like R plots within
49RStudio, R Markdown documents, and Shiny applications.
50
51## Getting Started
52
53First, install the package from GitHub as follows:
54
55``` r
56devtools::install_github("rstudio/r2d3")
57```
58
59Next, install the [preview release of RStudio
60v1.2](https://www.rstudio.com/rstudio/download/preview/) of RStudio (you
61need this version of RStudio to take advantage of various integrated
62tools for authoring D3 scripts with r2d3).
63
64Once you’ve installed the package and the RStudio v1.2 preview release
65you have the tools required to work with **r2d3**. Below, we’ll describe
66basic workflow within RStudio and techniques for including
67visualizations in R Markdown and Shiny applications.
68
69## About D3
70
71Creating data visualizations with **r2d3** requires lots of custom SVG
72graphics programming (similar to creating custom grid graphics in R).
73It’s therefore a good fit when you need highly custom visualizations
74that aren’t covered by existing libraries. If on the other hand you are
75looking for pre-fabricated D3 / JavaScript visualizations, check out the
76packages created using [htmlwidgets](http://www.htmlwidgets.org), which
77provide a much higher level interface.
78
79If you are completely new to D3, you may also want to check out the
80article on [Learning
81D3](https://rstudio.github.io/r2d3/articles/learning_d3.html) before
82proceeding further.
83
84## D3 Scripts
85
86To use **r2d3**, write a D3 script and then pass R data to it using the
87`r2d3()` function. For example, here’s a simple D3 script that draws a
88bar chart (“barchart.js”):
89
90``` js
91// !preview r2d3 data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)
92
93var barHeight = Math.floor(height / data.length);
94
95svg.selectAll('rect')
96  .data(data)
97  .enter().append('rect')
98    .attr('width', function(d) { return d * width; })
99    .attr('height', barHeight)
100    .attr('y', function(d, i) { return i * barHeight; })
101    .attr('fill', 'steelblue');
102```
103
104To render the script within R you call the `r2d3()` function:
105
106``` r
107library(r2d3)
108r2d3(data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20), script = "barchart.js")
109```
110
111Which results in the following visualization:
112
113<img src="tools/README/bar_chart.png" class="illustration" width=600/>
114
115### D3 Variables
116
117Note that data is provided to the script using the `data` argument to
118the `r2d3()` function. This data is then automatically made available to
119the D3 script. There are a number of other special variables available
120within D3 scripts, including:
121
122  - `data` — The R data converted to JavaScript.
123  - `svg` — The svg container for the visualization
124  - `width` — The current width of the container
125  - `height` — The current height of the container
126  - `options` — Additional options provided by the user
127  - `theme` — Colors for the current theme
128
129When you are learning D3 or translating D3 examples for use with R it’s
130important to keep in mind that D3 examples will generally include code
131to load data, create an SVG or other root element, and establish a width
132and height for the visualization.
133
134On the other hand with **r2d3**, these variables are *provided
135automatically* so do not need to be created. The reasons these variables
136are provided automatically are:
137
1381)  So that you can dynamically bind data from R to visualizations; and
139
1402)  So that **r2d3** can automatically handle dynamic resizing for your
141    visualization. Most D3 examples have a static size. This if fine for
142    an example but not very robust for including the visualization
143    within a report, dashboard, or application.
144
145## D3 Preview
146
147The [RStudio v1.2 preview
148release](https://www.rstudio.com/rstudio/download/preview/) of RStudio
149includes support for previewing D3 scripts as you write them. To try
150this out, create a D3 script using the new file menu:
151
152<img src="tools/README/new_script.png" class="screenshot" width=600/>
153
154A simple template for a D3 script (the barchart.js example shown above)
155is provided by default. You can use the **Preview** command
156(Ctrl+Shift+Center) to render the
157visualization:
158
159<img src="tools/README/rstudio_preview.png" class="screenshot" width=600/>
160
161You might wonder where the data comes from for the preview. Note that
162there is a special comment at the top of the D3 script:
163
164``` js
165// !preview r2d3 data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20)
166```
167
168This comment enables you to specify the data (along with any other
169arguments to the `r2d3()` function) to use for the preview.
170
171## R Markdown
172
173You can include D3 visualizations in an R Markdown document or R
174Notebook. You can do this by calling the `r2d3()` function from within
175an R code chunk:
176
177<pre><code>---
178output: html_document
179---
180
181&#96``{r}
182library(r2d3)
183r2d3(data=c(0.3, 0.6, 0.8, 0.95, 0.40, 0.20), script = "barchart.js")
184&#96``</code></pre>
185
186<img src="tools/README/bar_chart.png" class="illustration" width=600/>
187
188You can also include D3 visualization code inline using the `d3` R
189Markdown engine:
190
191<pre><code>&#96``{r setup}
192library(r2d3)
193bars &lt;- c(10, 20, 30)
194&#96``</code></pre>
195
196<pre><code>&#96``{d3 data=bars, options=list(color = 'orange')}
197svg.selectAll('rect')
198  .data(data)
199  .enter()
200    .append('rect')
201      .attr('width', function(d) { return d * 10; })
202      .attr('height', '20px')
203      .attr('y', function(d, i) { return i * 22; })
204      .attr('fill', options.color);
205&#96``</code></pre>
206
207<img src="tools/README/rmarkdown-1.png" class="illustration" width=600/>
208
209Note that in order to use the `d3` engine you need to add
210`library(r2d3)` to the setup chunk (as illustrated above).
211
212## Shiny
213
214The `renderD3()` and `d3Output()` functions enable you to include D3
215visualizations within Shiny applications:
216
217``` r
218library(shiny)
219library(r2d3)
220
221ui <- fluidPage(
222  inputPanel(
223    sliderInput("bar_max", label = "Max:",
224                min = 0.1, max = 1.0, value = 0.2, step = 0.1)
225  ),
226  d3Output("d3")
227)
228
229server <- function(input, output) {
230  output$d3 <- renderD3({
231    r2d3(
232      runif(5, 0, input$bar_max),
233      script = system.file("examples/baranims.js", package = "r2d3")
234    )
235  })
236}
237
238shinyApp(ui = ui, server = server)
239```
240
241<img src="tools/README/baranim-1.gif" class="illustration" width=600/>
242
243See the article on [Using r2d3 with
244Shiny](https://rstudio.github.io/r2d3/articles/shiny.html) to learn more
245(including how to create custom Shiny inputs that respond to user
246interaction with D3 visualizations).
247
248## Learning More
249
250To learn the basics of D3 and see some examples that might inspire your
251own work, check out:
252
253  - [Learning
254    D3](https://rstudio.github.io/r2d3/articles/learning_d3.html)255    Suggested resources for learning how to create D3 visualizations.
256
257  - [Gallery of
258    Examples](https://rstudio.github.io/r2d3/articles/gallery.html)259    Learn from a wide variety of example D3 visualizations.
260
261For next steps on learning on how to use **r2d3**, see these articles:
262
263  - [R to D3 Data
264    Conversion](https://rstudio.github.io/r2d3/articles/data_conversion.html)
265    — Customize the conversion of R objects to D3-friendly JSON.
266
267  - [D3 Visualization
268    Options](https://rstudio.github.io/r2d3/articles/visualization_options.html)
269    — Control various aspects of D3 rendering and expose user-level
270    options for your D3 script.
271
272  - [Development and
273    Debugging](https://rstudio.github.io/r2d3/articles/development_and_debugging.html)
274    — Recommended tools and workflow for developing D3 visualizations.
275
276  - [Publishing D3
277    Visualizations](https://rstudio.github.io/r2d3/articles/publishing.html)
278    — Publish D3 visualizations as HTML, a static PNG image, or within R
279    Markdown documents and Shiny applications.
280
281Once you are familar with the basics, check out these articles on more
282advanced topics:
283
284[Using r2d3 with
285Shiny](https://rstudio.github.io/r2d3/articles/shiny.html) — Deriving
286insights from data is streamlined when users are able to modify a Shiny
287input, or click on a D3 visualization, and that action produces new
288results.
289
290  - [CSS and JavaScript
291    Dependencies](https://rstudio.github.io/r2d3/articles/dependencies.html)
292    — Incorporating external CSS styles and JavaScript libraries into
293    your visualizations.
294
295  - [Advanced Rendering with
296    Callbacks](https://rstudio.github.io/r2d3/articles/advanced_rendering.html)
297    — An alternate way to organize D3 scripts that enables you to
298    distinguish between initialization and re-rendering based on new
299    data, as well as handle resizing more dynamically.
300
301  - [Package
302    Development](https://rstudio.github.io/r2d3/articles/package_development.html)
303    – Create re-usable D3 visualizations by including them in an R
304    package.
305