Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

2 Knitr/R Markdown/Rstudio issues: Highcharts and Morris.js

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

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.

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

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 :

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

NOTE: This is the same solution I posted in the knitr google group.

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

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.

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...