MYj*_*Yjx 11 r d3.js r-markdown
我的问题是我想将d3.js可视化集成到我的markdown而不是指向外部网站上的可视化的链接.有没有办法实现这一目标?
要完成向d3.v3.min.js
我们的Rmd 添加非本地javascript ,有几种方法可以做到这一点.如果您希望包含本地副本d3
,则更容易.
这是我最喜欢的方式.如果由于某种原因,你想看到其他人,我会很乐意向他们展示. 注意:我还在试验.
---
title: "rmarkdown example with external js"
output:
html_document:
self_contained: false
keep_md: true
includes:
in_header: "header_include_d3.html"
---
Let's create a very basic d3 graph using data from R. since the graph is d3, we will need the d3.js file for the graph to render.
```{r results='asis'}
cat('
<script>
d3.select("body").append("p").text("d3 made me")
</script>
')
```
<script>
// from https://www.dashingd3js.com/svg-paths-and-d3js
//The data for our line
var lineData = [ { "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];
//This is the accessor function we talked about above
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("linear");
//The SVG Container
var svgContainer = d3.select("body").append("svg")
.attr("width", 200)
.attr("height", 200);
//The line SVG Path we draw
var lineGraph = svgContainer.append("path")
.attr("d", lineFunction(lineData))
.attr("stroke", "blue")
.attr("stroke-width", 2)
.attr("fill", "none");
</script>
Run Code Online (Sandbox Code Playgroud)
然后在与.Rmd文件相同的目录中保存
<script src = "http://d3js.org/d3.v3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
到我打电话的文件header_include_d3.html
或你想要的任何名字.如果更改了名称,只是一定要更改参考includes
在yaml
您的RMD的.
正如我之前所说的,如果您想在本地使用d3.js,这会更容易.
此外,<script src='...'></script>
如果您不是特别关注标题中的js,那么正文内部将会起作用.在这种情况下,只需将其包含在Rmd中的任何位置即可.
您现在拥有允许这样做的R2D3包!Rmardown 是在 R 中包含 D3 可视化的一种方式 https://rstudio.github.io/r2d3/articles/publishing.html#r-markdown