21249

embed rChart in Markdown

I am trying to embed a NVD3 chart in a Markdown document. I am on a Ubuntu64 system with RStudio 0.98.932, R 3.1.0, rCharts 0.4.2, the browser is Chrome.

The instructions/code from this link:

```{r} library(rCharts) library(knitr) opts_chunk$set(comment = NA, results = "asis", comment = NA, tidy = F) hair_eye_male = subset(as.data.frame(HairEyeColor), Sex == "Male") n1 <- nPlot(Freq ~ Hair, group = 'Eye', data = hair_eye_male, type = 'multiBarChart' ) n1$set(width = 600) # n1$show('iframesrc', cdn = TRUE) # option 1 # n1$show('inline', include_assets = TRUE, cdn = TRUE) # option 2 ```

Neither of the options [n1$show] work, I just get code in a browser. Is there another way of including NVD3 documents in Markdown?

Answer1:

This code is working for me. I am using ubuntu64 and same config you mentioned.

```{r, echo=FALSE,results='asis',comment=NA} library(rCharts) hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male") n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, type = "multiBarChart") n1$show('iframesrc',cdn=TRUE) ```

Note : you <strong>must write results='asis' and comment = NA</strong> in <strong>chunk options</strong> and not use opts_chunk$set as you have in your code block and what you pasted to copy.com.

Answer2:

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.

Source:Knitr HTML in R Markdown

Recommend

  • Display two rCharts NVD3 figures next to each other in rmarkdown
  • rcharts: nPlot Formating x-axis with dates
  • Reorder factor in MultiBarChart with NVD3 rCharts
  • Overlapping date range MySQL
  • R shiny app with rCharts
  • Why is my script failing intermittently?
  • How to connect from Linux to Android Bluetooth socket
  • Generating linear combination of a matrix
  • How does this compute ? I am trying to understand how the values of H get assigned in the list
  • JTable Alert on invalid value
  • Duplicate Class in Dart
  • Forcing variable to reassign (Prolog)
  • sql query to select record having same id but different value in two columns
  • XMPP push notifications causing problems (delay + duplications) in messages
  • why doesn't this visNetwork in R show edge
  • Update two sets of radiobuttons - shiny
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • Find unique tuples in a relation represented by a BDD
  • How do I generate a Sine Sweep in Java (Android)
  • How to Divide an array on c#?
  • python - calculate orthographic similarity between words of a list
  • rapply over a nested list in R
  • How can I get the full list of running processes on a Mac from a python app
  • Use tryCatch within R loop
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • wxPython: displaying multiple widgets in same frame
  • R - Combining Columns to String Based on Logical Match
  • Linq Objects Group By & Sum
  • Read text file and split every line in MSBuild
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • KeystoneJS: Relationships in Admin UI not updating
  • trying to dynamically update Highchart column chart but series undefined
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal