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 :

Tal Galili said...

Great post Markus :)

Mark said...

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

Yihui Xie said...

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

Markus Gesmann said...

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

wes said...

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

Markus Gesmann said...

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

Jared said...

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.

ikuyasu said...

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?

ikuyasu said...

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.

Markus Gesmann said...

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

Dylan Beaudette said...

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.

Markus Gesmann said...

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

Enrique Garcia said...

 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.  

Markus Gesmann said...

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

Enrique Garcia said...

 problem solved....coding error!

wes said...

Thanks! Now to learn pandoc... 

Alberto said...

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

Markus Gesmann said...

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.

Moe said...

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

Moe said...

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

Xiong Xiong said...

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?

Markus Gesmann said...

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

Xiong Xiong said...

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

Markus Gesmann said...

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

Xiong Xiong said...

Thank you Markus, will try it out.

Markus Gesmann said...

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

Post a Comment