Interactive reports in R with knitr and RStudio

26 comments
Last Saturday I met the guys from RStudio at the R in Finance conference in Chicago. I was curious to find out what RStudio could offer. In the past I have used mostly Emacs + ESS for editing R files. Well, and what a surprise it was. JJ, Joe and Josh showed me a preview of version 0.96 of their software, which adds a close integration of Sweave and knitr to RStudio, helping to create dynamic web reports with the new R Markdown and R HTML formats more easily.

Screen shot of RStudio with a knitr file (*.Rmd) in the top left window.
Notice also the integrated knitr button.
You probably have come across Sweave in the past, but knitr is a fairly new package by Yihui Xie that brings literate programming to a new level. In particular the markdown approach allows me to create web content really quickly, without worrying to much about layout and R formatting. I begin to wonder if PDF and paper will be replaced by tablets and HTML5 in the future.

Here is a simple example. The knitr source code is available on Github.

26 comments :

  1. Looks awesome-- I'm newly discovering the wonders of knitr for the first time myself-- thanks for the examples!

    ReplyDelete
  2. Fabulous examples! Thanks for exploring the new possibilities of knitr!

    One minor point: you can remove suppressPackageStartupMessages() and add a chunk option message=FALSE instead so people won't be too surprised by this super long function name :)

    ReplyDelete
  3. Thanks for pointing this out, and also for your knitr package.

    ReplyDelete
  4. I am quite new to this, though, your example was so well done that I was able to reproduce it.  Is it possible to include latex tables in the output as well?  When I attempted it the output was of latex code rather than latex output. 

    If you had time for a simple example it would be appreciated.

    Cheers,
    Wes

    ReplyDelete
  5. Hi Wes, have you seen the following discussion entry?
    https://groups.google.com/group/knitr/browse_thread/thread/90acfbfc7a01e066?pli=1

    ReplyDelete
  6. Awesome post, I am going to try to build a dashboard based on what you showed.

    Also, there is indeed a spell checker built into RStudio, so you're all set with that.

    ReplyDelete
  7. Great example, and wonderful work! I got to reproduce in html previwed on Rstudio.
    My question is how you upload this page on blogspot (blogger)?
    Did you just copy the body part of html and paste it on a blog post manually keeping
    the image format as base64?

    ReplyDelete
  8. I did some experiments; Manually copying and pasting a part from head to the end of body instead of body part produces the (almost) identical display to what Rstudio produces. It even includes googlevis chart as it is designed to work! I don't know how that works but it's amazing. I wonder if there is a way to make this workflow a bit more automatic from Rstudio by typing a command in R console. There seems exit a way for Wordpress using Rwordpress package, but I have not found any posts for blogger.

    ReplyDelete
  9. The easiest way for me is to host the HTML output from knitr on a public folder, e.g. via Dropbox, and to to include the page into a blog post via an iframe tag i nHTML. The googleVis package vignette has more details on this in section 3.3, see http://cran.r-project.org/web/packages/googleVis/vignettes/googleVis.pdf

    ReplyDelete
  10. Dylan Beaudette6 August 2012 17:00

    Excellent examples. However, is there any way to get a stand-alone HTML file from and .Rhtml document via command-line operation? Using knit() always seems to create and HTML file that depends on external images.

    ReplyDelete
  11. Hi Dylan,
    Using googleVis for the charts actually gets around your problem. The output of the above knitr file is just one html file. The charts are rendered in the browser. See also  my example for a presentation done with knitr and googleVis: http://lamages.blogspot.co.uk/2012/05/interactive-html-presentation-with-r.html. The whole output is one html file only again.I hope this helps.Markus

    ReplyDelete
  12. Enrique Garcia13 August 2012 18:18

     Hello,

    I am trying to run the code that you provide above and keep getting an error saying that the functions supressPackageStartupMessages and gvisScatterChart could not be found.  Needless to say I was not able to produce the first interactive chart you provided as an example.  I have both the knitr and googleVis packages installed and loaded.  I am running the latest version of R Studio (0.96.316) and R (2.15.1).  I also added the working directory to my list of trusted locations in the global security settings of Flash player as you suggested.  Any help resolving this would be greatly appreciated.  

    ReplyDelete
  13. Hi Enrique,

    I believe you are trying to copy and paste the code from this post directly. You have to use the underlying source code, wich is hosted on GitHub. Here is the link agai, same as above https://gist.github.com/2704646

    I hope this helps. 

    Markus

    ReplyDelete
  14. Enrique Garcia13 August 2012 18:56

     problem solved....coding error!

    ReplyDelete
  15. Thanks! Now to learn pandoc... 

    ReplyDelete
  16. Hi Markus,

    Thank you for your brilliant posts!

    copied and paste the code, everything is fine a part the motion chart!

    I did make sure that the folder where I saved the html file is authorized by Flash, but no luck... This is a snapshot of what I see: just a blank space.

    Any suggestion?

    Cheers,
    Alberto

    ReplyDelete
  17. Please send me the version details of R and googleVis, plus the R code and HTML output via email and I will take a look at it. I have only today uploaded version 0.3.3 of googleVis to CRAN, you may want to test your code with that version first.

    ReplyDelete
  18. Hi Markus,
    This is a very useful post. Thanks for sharing!
    I tried to re-create the report on my system. But I'm not getting the cool colored boxes with the formatted R codes inside them. I'm just getting plain text. Any clues why this is happening?

    Thanks
    Moe

    ReplyDelete
  19. Thanks for your timely response Markus! I did knit an Rmd file. I am using markdownToHTML() following a knit() call. The fragment.only parameter was set to TRUE. I changed it to FALSE and now I have the neat boxes:
    markdownToHTML("test.md", "test.html",fragment.only = FALSE)
    problem solved. I should use knit2html as well and see how it works.
    Keep up the great work

    -Moe

    ReplyDelete
  20. Hi Mark, I am doing an application by R shiny+googleVis. One of the functions is to let users download the plot and statistics. Is it possible export gooleVis plot to pdf by knitr in Sweave?

    ReplyDelete
  21. Hi Xiong Xiong,

    Unfortunately not, see the FAQ section of the googleVis vignette: http://cran.r-project.org/web/packages/googleVis/vignettes/googleVis.pdf

    Markus

    ReplyDelete
  22. Hi, thank you for quick response. And still not possible if I only need static gvisScatterCharter without animation?

    ReplyDelete
  23. Well, there seems to be a hack to convert SVG charts, such as gvisScatterChart into PNG: https://gist.github.com/battlehorse/1333906

    ReplyDelete
  24. Thank you Markus, will try it out.

    ReplyDelete
  25. It would be great if you would share your experience later.

    ReplyDelete