问题
I am having issues rendering an rChart made with 'nPlot' when I knit an R Markdown document to html.
I followed the solution discussed in this question, but it was unsuccessful.
Here is my .Rmd code
```{r, echo=FALSE}
library(knitr)
```
---
title: "Untitled"
author: "Test"
date: "01/23/2015"
output: html_document
---
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.
# Here is an rChart
```{r, echo=FALSE, results='asis', comment=NA}
library(rCharts)
m2 <- nPlot(speed ~ dist, data = cars, type = "scatterChart")
m2$show('iframesrc', cdn = TRUE)
```
That was an rChart
Here is a link to the html document from that code. I produced and authored this in RStudio and the rendering fails to show up both on my local machine and when uploaded to Dropbox.
When I run the following code in console and save as an html, I get this rendering.
library(rCharts)
m2 <- nPlot(speed ~ dist, data = cars, type = "scatterChart")
m2$save('test3.html', standalone = TRUE)
回答1:
GOT IT.
see this answer: Ramnath layin' it down
(chest-swelling feeling of satisfaction quickly deflated upon realization we were just looking at outdated tutorials / walkthroughs...)
last line should be
n1$print('iframesrc', cdn =TRUE, include_assets=TRUE)
I think most of the tutorials out there are using an old version or something. But the above works for me, so give it a shot.
then knit, then you're good to go. Also make sure you're rCharts library is up to date
install_github("ramnathv/rCharts")
回答2:
I'm adding an updated answer here because I struggled for a long time with many outdated tutorials to get this to actually work. Also, the current answer here did not work for me.
This does work...
```{r set-options, echo=FALSE, cache=FALSE}
options(RCHART_WIDTH = 1000, RCHART_HEIGHT = 400)
```
```{r, echo=FALSE, cache=T, results='asis', comment=NA}
p1 <- nPlot(mpg ~ wt, group = 'cyl', data = mtcars, type = 'scatterChart')
p1$print('chart1', include_assets=T)
```
```{r, echo=FALSE, cache=T, results='asis', comment=NA}
hair_eye = as.data.frame(HairEyeColor)
p2 <- nPlot(Freq ~ Hair, group = 'Eye', data = subset(hair_eye, Sex == "Female"), type = 'multiBarChart')
p2$print('chart2', include_assets=T)
```
Note:
- I needed to set
results='asis'
andcomment=NA
in the chunk with the chart code, not the options block at the top. cdn=T
caused errors for me. R was looking for a public file and could not find it.- The charts each need a unique name or they will override or chart on top of each other.
- You can update the height and width of your charts in the options chunk
- I have R 3.1.2, rCharts_0.4.5, rmarkdown_0.7
回答3:
You can save your rChart plot as a html and then include it to the RMarkdown document with shiny::includeHTML("plot.html")
. This worked for me.
来源:https://stackoverflow.com/questions/28120267/rchart-in-r-markdown-doesnt-render