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.
Sometimes you may get an error message about SSL certificate verification, and publishing wouldn’t work. If that happens. Locate your RProfile file on your computer and add this line to the end:
options(rpubs.upload.method = "internal")
Save the file and restart your RStudio for RProfile changes to take action.
First you may have to include all the code that is required for your rCharts to work (any data manipulations, library calls, etc.).
library(rCharts)
setwd("C:/Users/kanstantsyia/Documents/")
milbases <- read.csv("data/milbases.csv")
reports <- read.csv("data/ufo_reports.csv")
merged <- merge(reports, milbases, by="State")
ufoplot <- hPlot(x="Total", y = "UFOReports", data = merged, type = "bubble",
title = "UFO Reports and Total number of Military Bases by State",
subtitle = "Size of bubble - number of AirForce Bases",
size = "AirForce")
## Warning: Observations with NA has been removed
ufoplot$chart(zoomType = "xy")
ufoplot$tooltip( formatter = "#! function() { return 'All Miliary Bases: ' + this.point.x + '<br />' +
'UFO Reports: ' + this.point.y + '<br />'; } !#")
ufoplot$xAxis(title = list(text = "Total Number of Military Bases in State"))
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)
The data files for this visualization were downloaded from online resouces. Within this Rmd document, the data is merged together and put into an rCharts plot (Highcharts package).
This data set is contained in file ufo_reports.csv The data was taken from the National UFO Reporting Center(Index by State/Province) Link to original: http://www.nuforc.org/webreports/ndxloc.html
This data set is contained in file milbases.csv This data set is an aggregate derived from data on NASAA website Link to original: http://www.nasaa-home.org/baselocs.htm