RMarkdown and RCharts

♀尐吖头ヾ 提交于 2019-12-02 18:59:34

问题


When I run this RMarkdown code:

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

I get this: http://rpubs.com/rajesh06/test_Rmd

I also tried the "self-contined: no" option by changing to this:

output: 
  html_document:
    self-contained: no

but that did not seem to help.

Any ideas?


回答1:


In my work I have a Mac and $show() works properly. I tried some of the code at my home's PC and for some reason $show() did not work. However, there is an easy way around to solve this issue using the $save() function:

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

You can find the discussion I got this solution from here: https://github.com/ramnathv/rCharts/issues/373

I recommend you to use the $save() function as it actually allows adding controls to the rCharts in standalone html which $show() does not.




回答2:


The more general solution (see this thread) is to use:

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


来源:https://stackoverflow.com/questions/29329598/rmarkdown-and-rcharts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!