RMarkdown和RCharts

时间:2015-03-29 13:20:36

标签: r r-markdown rcharts

当我运行此RMarkdown代码时:

---
title: "test"
output: html_document
---

```{r cache=FALSE}
library(rCharts)
library(knitr)
opts_chunk$set(comment = NA, results = "asis", comment = NA, tidy = F)

hair_eye = as.data.frame(HairEyeColor)
p2 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')
p2$chart(color = c('brown', 'blue', '#594c26', 'green'))
p2$show('inline', include_assets = TRUE, cdn = TRUE)
```

我明白了: http://rpubs.com/rajesh06/test_Rmd

我也试过了自我约束:没有&#34;选项改为:

output: 
  html_document:
    self-contained: no

但这似乎没有帮助。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在我的工作中,我有一台Mac和$ show()正常工作。 我在家里的电脑上尝试了一些代码,由于某些原因,$ show()无效。 但是,使用$ save()函数可以轻松解决此问题:

---
title: "test"
output: html_document
---

```{r cache=FALSE}
library(rCharts)
library(knitr)
opts_chunk$set(comment = NA, results = "asis", comment = NA, tidy = F)

hair_eye = as.data.frame(HairEyeColor)
p2 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')
p2$chart(color = c('brown', 'blue', '#594c26', 'green'))
p2$save("p2.html", standalone = TRUE)
```


<iframe src="p2.html" align="center" width="900" height="600" frameBorder="0"></iframe>

您可以在此处找到我从此处获得此解决方案的讨论: https://github.com/ramnathv/rCharts/issues/373

我建议你使用$ save()函数,因为它实际上允许在独立的html中为rCharts添加控件,$ show()没有。

答案 1 :(得分:1)

更通用的解决方案(请参阅this主题)是使用:

p1$show('inline', include_assets = TRUE, cdn = TRUE)

e.g。

```{r results = 'asis', comment = NA, cache = F}
library(rCharts)
n1 <- nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
n1$show('inline', include_assets = TRUE, cdn = TRUE)
```