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:
- IFrame
- Inline
You can view the source of this slide deck here
Ramnath Vaidyanathan
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:
You can view the source of this slide deck here
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)
By default, rCharts will save the plot to file and embed as an iframe
.
n1 # equivalent to n1$show('iframe') in Rmd
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)
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"))
plot(Bar1, tag = "chart")
print(Bar1, tag = "html", file = "gvis_iframe.html")
cat("<iframe src='gvis_iframe.html'></iframe>")