Embedding rCharts

Ramnath Vaidyanathan
24-Dec-2013

Outline

This is a short demo on how to embed visualizations created using rCharts in an RStudio Presentation (called RPres).

Make sure to set the chunk options results = "asis" and comment = NA so that the html output is rendered asis by knitr. Set cache = F on this setup chunk to ensure that this chunk is always evaluated.

require(knitr)
opts_chunk$set(results = "asis", comment = NA, tidy = F)

Highcharts

library(rCharts)
h1 <- hPlot(Pulse ~ Height, data = MASS::survey, 
  type = "scatter", group = "Exer"
)

Option 1: IFrame (Inline)

h1$show('iframesrc', cdn = TRUE)

Option 2: Inline

h1$show('inline', include_assets = TRUE, cdn = TRUE)

Polycharts

hair_eye = as.data.frame(HairEyeColor)
p2 <- rPlot(Freq ~ Hair, color = 'Eye', 
  data = hair_eye, 
  type = 'bar'
)
p2$facet(var = 'Eye', type = 'wrap', rows = 2)

IFrame Inline

p2$show('iframesrc', cdn = TRUE)

Inline

# NOTE HOW CSS INTERFERES WITH CHART
p2$show('inline', include_assets = TRUE, cdn = TRUE)

NVD3

hair_eye = as.data.frame(HairEyeColor)
p3 <- nPlot(Freq ~ Hair, group = 'Eye',
  data = subset(hair_eye, Sex == "Female"), 
  type = 'multiBarChart'
)
p3$chart(color = c('brown', 'blue', '#594c26', 'green'))

IFrame Inline

p3$show('iframesrc', cdn = TRUE)

Inline

p3$show('inline', include_assets = TRUE, cdn = TRUE)