How can I render an rChart in an Rmarkdown shiny document?

317 views Asked by At

I am programming an extensive data document that relies partly on some shiny features.

I would like to implement rCharts and I found out how to do it with a clean RMarkdown document.

I am running into trouble as soon as I want to turn on the Shiny features so that the rest of the document works as supposed to.

The code below works as it should

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

This does not work

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

So my question is how to embed an rChart into a shiny Rmarkdown document?

1

There are 1 answers

0
SabDeM On

Here is my solution even thought I am aware it is not the best.

First step:

Create an external html file that contains the rChart you want to include. Here is the code:

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

Where the save() function allows us to save the n1 rChart object to an external html file, obviously made up of pure HTML code. This example is from here.

I saved this file as grap.html in the same directory where we shall create the html to include in.

Second step:

create an RMarkdown file. I used the one of example provide by Rstudio. He point here is the option(s) includes:.

It allows to include a pure HTML at the end of the body of your HTML. If you want to add at the bottom I think you have to specify before_body: grap.html

It is not perfect but may be a start and trigger the ideas of other users. You can find other informations: 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.