【问题标题】:How can I render an rChart in an Rmarkdown shiny document?如何在 Rmarkdown 闪亮文档中呈现 rChart?
【发布时间】:2015-08-24 20:04:55
【问题描述】:

我正在编写一个扩展的数据文档,该文档部分依赖于一些闪亮的特性。

我想实现 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 文档中?

【问题讨论】:

    标签: r shiny rcharts


    【解决方案1】:

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

    第一步:

    创建一个外部 html 文件,其中包含您要包含的 rChart。代码如下:

    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 代码组成。这个例子来自here

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

    第二步:

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

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

    它并不完美,但可能是一个开始并引发其他用户的想法。其他信息可查询:http://rmarkdown.rstudio.com/html_document_format.html#html-fragments

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

    【讨论】:

      猜你喜欢
      • 2014-08-09
      • 2021-07-09
      • 2018-01-10
      • 2021-08-13
      • 2020-07-27
      • 2017-12-22
      • 2017-09-16
      • 1970-01-01
      • 2016-09-13
      相关资源
      最近更新 更多