googleVis 0.3.2 is released: Better integration with knitr
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
After last week’s kerfuffle I hope the roll out of googleVis version 0.3.2 will be smooth. To test the water I release this version into the wild here and if it doesn’t get shot down in the next days, then I shall try to upload it to CRAN. I am mindful of the CRAN policy, so please get in touch or add comments below if you find any show stoppers.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
So what’s new in googleVis 0.3.2?
The default behaviour of the functionsprint.gvis
and plot.gvis
can be set via options()
. Now this doesn’t sound too exciting but it can be tremendously helpful when you write Markdown files for
knitr
. Here is why:The default googleVis
plot
method opens a browser window to display gvis-objects. That’s great when you work with R and googleVis in an interactive and explorative way, but if you like to include the plots into a knitr Markdown file then you would have to change those statements to print(x, tag="chart")
, as explained in an earlier post. Including googleVis output in knitr with plot statement
With version 0.3.2 of googleVisplot.gvis
gained the argument 'tag'
, which works similar to the argument of the same name in print.gvis
. By default the tag argument is NULL
and plot.gvis
has the same behaviour as in the previous versions of googleVis. Change the tag to 'chart'
and plot.gvis
will produce the same output as print.gvis
. That means instead of opening a browser window plot.gvis
will return the HTML code of the googleVis chart.And here is the real trick, if tag is not set explicitly in
plot.gvis
then it will use the value set in options(gvis.print.tag)
. Thus, if I set the gvis.plot.tag
value to 'chart'
in options()
then all following plot statements will return the HTML code of the chart when the file is parsed with knitr
. Set it back to NULL
via options(gvis.plot.tag=NULL)
and the old behaviour of plot.gvis
has been restored.Here is the example of the updated help file to
plot.gvis
and package vignette. The Markdown and R code is available below. I hope this illustrates the concept clearly.R Markdown file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Markdown example with knitr and googleVis | |
=========================================== | |
This is a little Markdown example file. | |
Set the googleVis options first. | |
In this case change the behaviour of plot.gvis | |
```{r setOptions, message=FALSE} | |
library(googleVis) | |
op <- options(gvis.plot.tag='chart') | |
``` | |
The following plot statements will automatically return the HTML | |
required for the 'knitted' output. | |
## Combo chart | |
```{r ComboExample, results='asis', tidy=FALSE} | |
## Add the mean | |
CityPopularity$Mean=mean(CityPopularity$Popularity) | |
CC <- gvisComboChart(CityPopularity, xvar='City', | |
yvar=c('Mean', 'Popularity'), | |
options=list(seriesType='bars', | |
width=450, height=300, | |
title='City Popularity', | |
series='{0: {type:\"line\"}}')) | |
plot(CC) | |
``` | |
Example of gvisComboChart with R code shown above. | |
## Place two charts next to each other | |
```{r gvisMergeExample, results='asis', echo=FALSE} | |
Geo <- gvisGeoChart(Exports, locationvar='Country', colorvar='Profit', | |
options=list(height=300, width=350)) | |
Tbl <- gvisTable(Exports, options=list(height=300, width=200)) | |
plot(gvisMerge(Geo, Tbl, horizontal=TRUE)) | |
`````` | |
Example of a gvisGeoChart with gvisTable and R code hidden. | |
## Motion Chart | |
```{r MotionChartExample, results='asis', tidy=FALSE} | |
M <- gvisMotionChart(Fruits, 'Fruit', 'Year', | |
options=list(width=400, height=350)) | |
plot(M) | |
``` | |
Please note that the Motion Chart is only displayed when hosted on a | |
web server, or is placed in a directory which has been added to the | |
trusted sources in the [security settings of Macromedia] | |
(http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html). | |
See the googleVis package vignette for more details. | |
```{r resetOptions} | |
## Set options back to original options | |
options(op) | |
``` |
R Code to knit HTML output
The code below shows how the Markdown file can be converted into a HTML file and displayed in a browser. If you use RStudio then most of it is happening in the background when you hit the “knit HTML” button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googleVis) ## Version >= 0.3.2 required | |
library(knitr) | |
library(markdown) | |
library(RCurl) | |
## URL to the Markdown example file on github:gist | |
gist <- "https://raw.github.com/gist/3968910/4633a98fdc5193eb7da156059d182e61ccbfd4a8/MarkdownExampleWithGoogleVis.Rmd" | |
knitrRmd <- paste(readLines(textConnection(getURL(gist))), collapse="\n") | |
## Write the content of knitrRmd into a Rmd-file, knit it and convert it | |
## into a html output. Finally show the file with the R-help http | |
## server, this will ensure that also the motion chart is visible. | |
wd <- getwd() | |
setwd(tempdir()) | |
fn=tempfile() | |
fn.Rmd <- paste(fn, ".Rmd", sep="") | |
fn.md <- paste(fn, ".md", sep="") | |
fn.html <- paste(fn, "-out.html", sep="") | |
## Write R Markdown into a file | |
cat(knitrRmd, file=fn.Rmd) | |
render_markdown() | |
knit(fn.Rmd, fn.md) | |
knit2html(fn.md) | |
## Open HTML file in browser | |
plot.gvis(fn.html) |
The following few lines replicate the whole example, sourcing the above gists.
URL <- "https://raw.github.com/gist/3968939/f137ecefe6eef3358d2aef89080c06ba061be0c3/KnitMarkdownExample.R"
library(RCurl)
source(textConnection(getURL(URL)))
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.