This is a demonstration of embedding an rChart into an R-markdown document for publishing on RPubs. After you create an account on rpubs.com. Make sure to install knitr package, and then from RStudio, create a new R-markdown file. Insert your content, embed the rchart and click the button on top, saying “Knit HTML”. This will show your Rmd doc in RStudio or Browser. From the window that opens, choose option “Publish”. This will open RPubs in your browswer, where you’ll specify the title and description and finish publishing.
options(rpubs.upload.method = "internal")
setwd("C:\\Users\\fqi\\Documents")
You can also embed plots, for example:
data(iris)
library(knitr)
kable(iris[1:6,], digits = 2, align = 'c', caption = "IRIS Dataset")
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|---|---|---|---|
5.1 | 3.5 | 1.4 | 0.2 | setosa |
4.9 | 3.0 | 1.4 | 0.2 | setosa |
4.7 | 3.2 | 1.3 | 0.2 | setosa |
4.6 | 3.1 | 1.5 | 0.2 | setosa |
5.0 | 3.6 | 1.4 | 0.2 | setosa |
5.4 | 3.9 | 1.7 | 0.4 | setosa |
library(rCharts)
ufoplot <- hPlot(x="Sepal.Length", y = "Petal.Length", data = iris, type = "bubble",
title = "testing",
subtitle = "testing",
size = "Petal.Width",
group = "Species")
Then
ufoplot$chart(zoomType = "xy")
ufoplot$tooltip( formatter = "#! function() { return 'Sepal.Length: ' + this.point.x + '<br />' +
'Petal.Length: ' + this.point.y + '<br />'; } !#")
ufoplot$xAxis(title = list(text = "xxx"))
ufoplot$set(width = 800)
Set up the options to embed the plot:
library(knitr)
opts_chunk$set(comment = NA, results = 'asis', comment = NA, tidy = F)
Embed Option 1: Iframe Inline This first option is to embed the chart as an inline iframe. It has the advantage of keeping the html standalone, but isolating the chart from the html on the page, thereby avoiding css and js conflicts. However, this feature is not supported by IE and Opera.
library(rCharts)
ufoplot$show('iframesrc', cdn = TRUE)
Embed Option 2: Inline
This option embeds the chart inline in the html. It should work in all browsers that the charting library being used supports. However, it is susceptible to css and js conflicts.
library(rCharts)
ufoplot$show('inline', include_assets = TRUE, cdn = TRUE)