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

I'm currently trying to replicate several 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 it starts as a piece, the graph turns out beautifully, but when I use Knit HTML in the markdown method, it remains at the preview stage for some time, and when I finish it, it gives the message โ€œstatus 15โ€, which I donโ€™t understand what it is means and how it should be allowed.

The second chart I'm trying to make is the Morris.js chart in Markdown with knitr. I took my R code and put it in R Markdown, which looks like this:

 ```{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 pieces, it creates a good graph as I expected with an html file (file: /// C: /Users/ypetscher/AppData/Local/Temp/RtmpW4q3ka/filed284f137718.html); however, when I click on Knit HTML, I get a file that contains code but does not create a graph. In addition, when Google Chrome appears, I get an error:

"The webpage was not 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 problems. Thank you very much!

+4
source share
1 answer

NOTE. This is the same solution that I posted in the knitr ad group.

To make rCharts work with knit2html, you will need to use the print method with the argument include_assets = TRUE. This is because knitr will not automatically add the js and css assets needed for the rCharts chart. 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 are going to publish your chart on RPubs, otherwise JS and CSS assets will be sent from your local library.

Hope this helps.

+8
source

Source: https://habr.com/ru/post/1486810/


All Articles