如何在Rmarkdown闪亮文档中呈现rChart?

时间:2015-06-09 19:55:28

标签: r shiny rcharts

我正在编写一个广泛的数据文档,它部分依赖于一些闪亮的功能。

我想实现rCharts,我发现如何使用干净的RMarkdown文档来完成它。

我想在打开Shiny功能时遇到麻烦,以便文档的其余部分按原样运行。

下面的代码可以正常工作

---
title: "Paper 4"
author: "Kasper Christensen"
date: "Monday, June 01, 2015"
output: html_document
runtime: html
---

```{r, echo=FALSE,  results='asis', comment=NA, warning=FALSE, message=FALSE}

library(ggvis)
library(rCharts)
library(plotly)
library(stringr)
library(reshape2)
library(reshape)
require(rCharts)

.libPaths("C:/R/R-3.1.3/library/")

# data
ds <- read.csv("data/misc/FullDS_2015-01-13.csv")
ds <- subset(ds, X_golden == "false")
ds <- ds[c(4,8)]
ds <- as.data.frame(table(ds))

# plot
m2 <- nPlot(Freq ~ TARGET, group = "GROUP", data = ds, type = "multiBarChart")
m2$print('iframesrc', cdn =TRUE, include_assets=TRUE)
```

这不起作用

---
title: "Paper 4"
author: "Kasper Christensen"
date: "Monday, June 01, 2015"
output: html_document
runtime: shiny
---

```{r, echo=FALSE,  results='asis', comment=NA, warning=FALSE, message=FALSE}

library(ggvis)
library(rCharts)
library(plotly)
library(stringr)
library(reshape2)
library(reshape)
require(rCharts)

.libPaths("C:/R/R-3.1.3/library/")

# data
ds <- read.csv("data/misc/FullDS_2015-01-13.csv")
ds <- subset(ds, X_golden == "false")
ds <- ds[c(4,8)]
ds <- as.data.frame(table(ds))

# plot
m2 <- nPlot(Freq ~ TARGET, group = "GROUP", data = ds, type = "multiBarChart")
m2$print('iframesrc', cdn =TRUE, include_assets=TRUE)
```

所以我的问题是如何将rChart嵌入到闪亮的Rmarkdown文档中?

1 个答案:

答案 0 :(得分:1)

这是我的解决方案,即使我知道它不是最好的。

第一步:

创建一个包含您要包含的rChart的外部html文件。这是代码:

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart")

n1$print("chart3")

n1$save("grap.html")

save()函数允许我们将n1 rChart对象保存到外部html文件,显然由纯HTML代码组成。此示例来自widget

我将此文件保存为grap.html同一目录中,我们将在其中创建要包含的html。

第二步:

创建RMarkdown文件。我使用了Rstudio提供的示例之一。他指的是选项includes:

它允许在HTML正文的末尾包含纯HTML。如果你想在底部添加我认为你必须指定before_body: grap.html

它并不完美,但可能是一个开始并触发其他用户的想法。您可以找到其他信息:here

---
title: "Prova"
author: "SabDeM"
date: "09 giugno 2015"
output:
  html_document:
    includes:
        after_body: grap.html
---

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.