如何将交互式可视化添加到R markdown [英] How to add an interactive visualization to R markdown

查看:499
本文介绍了如何将交互式可视化添加到R markdown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想将d3.js可视化集成到我的markdown而不是指向外部网站上的可视化的链接。有没有办法实现这一点?

解决方案

要完成添加非本地JavaScript如 d3.v3。 min.js 到我们的Rmd,有几种方法来做。如果你正在寻找包括 d3 的本地副本,这将容易得多。



这是我最喜欢的方式。如果由于某种原因,你想看到其他人,我会很乐意向他们展示。 注意:我还在试验。

  --- 
title:rmarkdown示例与外部js
输出:
html_document:
self_contained:false
keep_md:true
包括:
in_header:header_include_d3.html
---

让我们使用R的数据创建一个非常基本的d3图形,因为图形是d3,我们需要d3.js文件来渲染图形。

```{r results ='asis'}

cat('
< script>
d3.select(body) .append(p)。text(d3 made me)
< / script>
')

```

< script>

//来自https://www.dashingd3js.com/svg-paths-and-d3js
//我们行的数据
var lineData = [{x :1,y:5},{x:20,y:20},
{x:40,y:10} y:40},
{x:80,y:5},{x:100,y:60}

//这是我们上面讨论的访问器函数
var lineFunction = d3.svg.line()
.x(function(d){return dx;})
.y(function(d){return dy;})
.interpolate(linear);

// SVG容器
var svgContainer = d3.select(body)。append(svg)
.attr(width,200)
.attr(height,200);

//线SVG绘制的路径
var lineGraph = svgContainer.append(path)
.attr(d,lineFunction(lineData))
.attr(stroke,blue)
.attr(stroke-width,2)
.attr(fill,none);

< / script>

然后在与此.Rmd文件相同的目录中,保存此

 < script src =http://d3js.org/d3.v3.min.js\"> ;</script> 

到我调用的文件 header_include_d3.html 或任何你想要的名称。如果您更改名称,只需确保更改Rmd的 yaml 中的 includes / p>

如前所述,如果您在本地使用d3.js,这将更容易。



另外,如果你不是特别关心你的js在你的身体里面,那么< script src ='...'>< / script> 标题。在这种情况下,只需将它包含在Rmd中的任何位置。


My question is I want to integrate a d3.js visualization to my markdown rather than a link pointing to the visualization on external website. Is there a way to achieve that?

解决方案

To accomplish adding nonlocal javascript such as d3.v3.min.js to our Rmd, there are a couple ways to do it. If you are looking to include local copy of d3, it is much easier.

This is my favorite way. If for some reason, you would like to see the others, I will be happy to show them. Note: I am still experimenting.

---
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>

then in the same directory as this .Rmd file, save this

<script src = "http://d3js.org/d3.v3.min.js"></script>

into a file I called header_include_d3.html or whatever name you would like. If you change the name, just be sure to change the reference in the includes in the yaml of your Rmd.

As I said before, this is much easier if you have d3.js locally that you would like to use.

Also, <script src='...'></script> inside the body will work if you are not particular about have your js in the header. In that case, just include it anywhere in the Rmd.

这篇关于如何将交互式可视化添加到R markdown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆