googleVis 0.3.2 is released: Better integration with knitr

2 comments
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.

So what's new in googleVis 0.3.2?

The default behaviour of the functions print.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 googleVis plot.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



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.



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)))

2 comments :

  1. Thanks Markus.

    I just downloaded the latest googleVis and when I run the following code from gvisMotionChart

    M1 <- gvisMotionChart(Fruits, idvar="Fruit", timevar="Year")plot(M1)

    I get an error message:
    Error in ifelse(interactive(), getOption("browser"), "false") :
    replacement has length zero



    I did get a warning saying the package was built under R version 2.15.2 and I am running 2.15.1. Somehow the 2.15.2 version is not available for download as of this moment.


    Thanks for all the work!


    Kelvin

    ReplyDelete
  2. I believe you are using version 0.3.0. Please download the updated package via the link provide above. The issue should be fixed with version 0.3.1 and greater. I will upload the updated version on CRAN in a few days. I hope this helps.

    ReplyDelete