Sunday, August 25, 2013

Using JavaScript visualization libraries with R

This is a short tutorial on knitr/markdown and JS visualization packages googleVis and rCharts. With these packages you can create web pages with interactive visualizations just using R. This will require minimal or no knowledge of HTML or JavaScript. 
You need to have the following R packages and their dependencies installed:
  • knitr
  • googleVis
  • rCharts (not on CRAN)
The tutorial is organized in four parts. First two parts introduce basics about knitr and markdown. The last parts are about using googleVis and rCharts packages in markdown documents. However, the tutorial does not have in-depth examples that show all the capabilities of rCharts and googleVis.

You can download the .Rmd files (or clone the repository from github) and run knit2html() on them in your R console, or if you are using RStudio you can click "knit HTML" button on the upper left corner.

The best way to go through the tutorial is to examine the code chunks and explanations in .Rmd files, and then check the HTML output from knit2html().

1. R and markdown

markdown_knitr.Rmd shows basics of markdown and knitr integration. These tools will help you create an HTML document using R. knit2html()output is here. In addition, R markdown basics are described here.

2. Customizing code output in R markdown documents

controlling_knitr.Rmd shows how code chunk output can be controlled by knitr options. knit2html() output is here

3. Using Google visualization API in R markdown documents

googleVis.Rmd shows how to use googleVis package in a markdown document.You can incorporate plots from Google Visualization API in your R markdown document, which will be converted to an HTML document by knit2html(), the HTML output is here.

4. Using multiple JS visualization libraries in R markdown documents

rCharts.Rmd shows how to use rCharts package in a markdown document. Using rCharts, you can incorporate various JS visualizations (such as Polychart, NVD3 etc.) on your HTML document. knit2html() output is here.


The project page is: http://al2na.github.io/Rmarkdown_JSviz/

6 comments:

  1. There's also http://christophergandrud.github.io/d3Network/

    ReplyDelete
    Replies
    1. I didn't know about this one, looks good. I will add it if I can find time.

      Delete
  2. You also may want to check out https://github.com/tdhock/animint

    A D3 interactive animation can be defined in R code using a list of ggplots with clickSelects and showSelected aesthetics.

    ReplyDelete
  3. Good Morning,

    I read with great interest your markdown example with knitr and googleVis.

    However trying to reproduce it in Rstudio and clicking on knithtml button I get
    Things like:

    Pie <- gvisPieChart(CityPopularity, options = list(width = 400, height = 200))
    plot(Pie)



    I wonder how I can get the actual graph output in the html document as you show in your output file. I have played with the options(gvis.plot.tag = “chart”) without success.

    I am sure I am missing something fairly obvious and thanks for any help.

    Best wishes, JL

    ReplyDelete
    Replies
    1. OK, figured it out. It doesn't work with the latest version of googleVis, which is 0.4.4, but works with 0.4.3 even on R 3.0.1. I installed the older version of googleVis from source, and it worked!

      Delete
  4. Hi JL, it works for me with the following configuration and packages:
    ## R version 3.0.0 (2013-04-03)
    ## Platform: x86_64-apple-darwin10.8.0 (64-bit)
    ##
    ## locale:
    ## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## attached base packages:
    ## [1] stats graphics grDevices utils datasets methods base
    ##
    ## other attached packages:
    ## [1] googleVis_0.4.3 knitr_1.4.1
    ##
    ## loaded via a namespace (and not attached):
    ## [1] digest_0.6.3 evaluate_0.4.7 formatR_0.9 RJSONIO_1.0-3
    ## [5] stringr_0.6.2 tools_3.0.0

    but it doesn't work with:

    R version 3.0.1 (2013-05-16)
    ## Platform: x86_64-apple-darwin10.8.0 (64-bit)
    ##
    ## locale:
    ## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    ##
    ## attached base packages:
    ## [1] stats graphics grDevices utils datasets methods base
    ##
    ## other attached packages:
    ## [1] knitr_1.4.1 googleVis_0.4.4
    ##
    ## loaded via a namespace (and not attached):
    ## [1] digest_0.6.3 evaluate_0.4.7 formatR_0.9 RJSONIO_1.0-3
    ## [5] stringr_0.6.2 tools_3.0.1

    So please do share if you find a fix. I think it is something to do with how .md files are converted to HTML.

    ReplyDelete