--- title: rCharts with Slidify author: Ramnath Vaidyanathan hitheme: solarized_light mode: standalone --- ## Note This is a short demo of how to embed a plot created using [rCharts](http;//rcharts.io) in a `knitr` document. In short, there are two ways to embed your plot: 1. IFrame 2. Inline You can view the source of this slide deck [here](index.Rmd) --- ## Create Plot Let us first create a plot that we would like to embed ```{r results = 'asis', comment = NA} library(rCharts) dat <- subset(as.data.frame(HairEyeColor), Sex == "Male") n1 <- nPlot(Freq ~ Hair, group = "Eye", data = dat, type = "multiBarChart") n1$set(width = 800, height = 350) ``` --- ## Embed Plot as IFrame By default, rCharts will save the plot to file and embed as an `iframe`. ```{r nvd3_iframe, fig.path = './', results = 'asis', comment = NA} n1 # equivalent to n1$show('iframe') in Rmd ``` ---- ## Embed Plot Inline You can also include the plot inline by invoking the `show` method with `inline` mode. Note that you need to set `include_assets = T` and `cdn = T` so that the js/css assets are included and delivered from an online CDN. ```{r nvd3_inline, results = 'asis', comment = NA, echo = 1} n1$show('inline', include_assets = TRUE, cdn = TRUE) cat("") ``` --- ## Create googleVis Chart ```{r results = 'asis', comment = NA, message = F, tidy = F} library(googleVis) df <- data.frame(country = c("US", "GB", "BR"), val1 = c(1,3,4), val2 = c(23,12,32)) Bar1 <- gvisBarChart(df, xvar="country", yvar=c("val1", "val2")) ``` --- ## Embed Plot Inline ```{r results = 'asis', comment = NA, message = F} plot(Bar1, tag = 'chart') ``` --- ## Embed Plot as IFrame ```{r message = F, results = 'asis', comment = NA} print(Bar1, tag = 'html', file = 'gvis_iframe.html') cat("") ```