rCharts with Slidify

Ramnath Vaidyanathan

Note

This is a short demo of how to embed a plot created using rCharts 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

Create Plot

Let us first create a plot that we would like to embed

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.

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.

n1$show("inline", include_assets = TRUE, cdn = TRUE)

Create googleVis Chart

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

plot(Bar1, tag = "chart")

Embed Plot as IFrame

print(Bar1, tag = "html", file = "gvis_iframe.html")
cat("<iframe src='gvis_iframe.html'></iframe>")