2 Knitr/R Markdown/Rstudio问题:Highcharts和Morris.js [英] 2 Knitr/R Markdown/Rstudio issues: Highcharts and Morris.js
问题描述
我目前正在尝试使用自己的数据复制几种不同类型的rCharts.第一个是带有以下代码的HighCharts图:
I'm presently trying to replicate a few different types of rCharts using my own data. The first is a HighCharts graph with the following code:
````{r}
setwd("C:/Users/ypetscher/Dropbox/R fun")
blah<-read.csv("g8a.csv")`
require(slidify)
require(rCharts)
require(rHighcharts)
```
```{r}
x<-data.frame(blah,stringsAsFactors=TRUE)
colnames(x)<-substr(colnames(x),2,5)
a<-rHighcharts:::Chart$new()
a$chart(type="column")
a$title(text="Percent of Students on Grade Level on G8 FCAT for Reading (1), Math (2), Writing (3), and Science (4)")
a$xAxis(categories=rownames(x))
a$yAxis(title=list(text="Percent Proficient"))
a$data(x)
```
当它作为一个块运行时,会很好地生成图形,但是当我在markdown中使用Knit HTML时,它会在预览阶段停留一段时间,而当我终止它时,它会显示状态15"消息,我不清楚这意味着什么以及应该如何解决.
When this is run as a chunk, the graph is produced nicely, but when I use Knit HTML in markdown, it sticks at the preview stage for a while and when I terminate it, it gives a "status 15" message, which I'm unclear what that means and how it should be resolved.
我正在尝试的第二张图是Markdown中带有knitr的Morris.js图.我将我的R代码放入R Markdown中,如下所示:
A second graph I'm trying is a Morris.js graph in Markdown with knitr. I took my R code and put into R Markdown which looks like:
```{r}
library(slidify)
library(knitr)
library(rCharts)
library(RColorBrewer)
library(reshape2)
setwd("C:/Users/ypetscher/Dropbox/R fun")
blah<-read.csv("g8.csv")
blah
```
```{r}
m2<-mPlot(x="year",y=colnames(blah)[-1],data=blah, type="Bar")
m2$set(barColors=brewer.pal(4,"Spectral"))
m2$set(xlab="Year")
m2$set(postUnits="%")
m2$set(hideHover="auto")
m2
```
当我运行这些块时,它会以我期望的html文件(file:///C:/Users/ypetscher/AppData/Local/Temp/RtmpW4q3ka/filed284f137718.html)生成一个漂亮的图形;但是,当我单击编织HTML"时,会获得一个包含代码的文件,但不会生成图形.此外,当谷歌浏览器启动时,我会收到错误消息:
When I run the chunks, it produces a nice graph the way I expected with an html file of (file:///C:/Users/ypetscher/AppData/Local/Temp/RtmpW4q3ka/filed284f137718.html); however, when I click on Knit HTML, I obtain a file which includes the code, but doesn't produce the graph. Additionally, when Google Chrome comes up I receive an error of :
找不到该网址的网页: 文件:///C:/Users/YPETSC~1/AppData/Local/Temp/Rtmpk1Pfbp/filee0c383670e0.html 错误6(net :: ERR_FILE_NOT_FOUND):文件或目录不能为 找到."
"No webpage was found for the web address: file:///C:/Users/YPETSC~1/AppData/Local/Temp/Rtmpk1Pfbp/filee0c383670e0.html Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found."
在诊断这些问题上的任何帮助将不胜感激.非常感谢!
Any help would be greatly appreciated in diagnosing these issues. Thank you much!
推荐答案
注意:这与我在knitr google组中发布的解决方案相同.
NOTE: This is the same solution I posted in the knitr google group.
要使rCharts与knit2html一起使用,您将需要使用带有参数include_assets = TRUE的print方法.这是因为knitr不会自动添加rCharts图所需的js和css资产.这是一个最小的工作示例.
To get rCharts to work with knit2html, you will need to use the print method with the argument include_assets = TRUE. This is because knitr will not add the js and css assets required by an rCharts plot automatically. Here is a minimal working example.
## MorrisJS with Knit2HTML
```{r results = 'asis', comment = NA}
require(rCharts)
data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
data = econ)
m1$set(pointSize = 0, lineWidth = 1)
m1$print('chart2', include_assets = TRUE)
```
请注意,如果要在RPubs上发布图表,则需要使用m1$print('chart2', include_assets = TRUE, cdn = TRUE)
,否则JS和CSS资产将从您的本地库中提供.
Note that you need to use m1$print('chart2', include_assets = TRUE, cdn = TRUE)
if you intend to publish your chart on RPubs, for otherwise the JS and CSS assets will be served from your local library.
希望这会有所帮助.
这篇关于2 Knitr/R Markdown/Rstudio问题:Highcharts和Morris.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!