Creating multi-tab reports with R and jQuery UI

December 29, 2015

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

by Matt Parker, Data Scientist at Microsoft

One of the great advantages of R's openness is its extensibility. R's abundant
packages are the most conspicuous example of that extensibility, and
Revolution R Enterprise is a powerful example of how far it can stretch.

But R is also part of an entire ecosystem of open tools that can be linked
together. For example,
Pandoc, and
combine to make R an incredible tool for dynamic reporting and reproducible
research. If your chosen output format is HTML, you've linked into yet another
open ecosystem with countless further extensions.

One of those extensions – and the focus of this post – is
jQuery UI.
jQuery UI makes a set of JavaScript's most useful moves available to developers
as a robust, easy-to-implement toolkit ideal for adding a bit of interactivity
to your knitr reports.


For example: it's easy to use
jQuery UI's Tabs widget
to split a long report across several tabs of a webpage. Tabs are great for
splitting complex reports up by topic, or for providing different types of
users with customized views of the results.

To get a sense of what this conversion might look like, here's
a simple R-Markdown report without tabs
(Rmd source):


… and the same report with tabs


Here's how I added tabs to the report.

1) First, I downloaded jQuery UI. Picking the right place to store the library
can be tricky, but as long as the jQuery UI files are accessible to knitr when
it's building the report, you'll be okay. For this demo report, I just unzipped
the files right next to the .rmd source.

2) Next, I added a few lines to the element of the report. Every
webpage has a element. knitr would typically build this for you, but
in this case we need to write it manually to be sure that the jQuery UI scripts
and CSS are linked in the HTML output.

  Reported Active Tuberculosis Cases in the United States, 1993-2013

3) Next, I created the navigation bar by creating an HTML chunk (div) with a
list inside of it (ul). Each item in that list (li) represents one tab
that I'd like the page to have. Finally, I make each of those list items a link
with a unique tag (),
and give the link a title (Nationally, By State, Treatment Completion).

Don't worry if you don't understand the HTML syntax here – you can just copy
and edit the code above.

4) Finally, I marked out which sections of R-Markdown I wanted to put on each
tab by surrounding that section with a div:

## Reported Active TB Cases in the United States, 1993-2013 ```{r nation} tbstats %>% group_by(Year) %>% summarise(n_cases = sum(Count)) %>% ggplot(aes(x = Year, y = n_cases)) + geom_line(size = 2) + labs(x = "Year Reported", y = "Number of Cases", title = "Reported Active TB Cases in the United States") + expand_limits(y = 0) ```

There are two crucial details here:
– the div has an id that corresponds to one of the tabs I've created
(href=#nation corresponds to

– the div is closed with a
tag. Without this, the entire report
would be included on the first tab.

5) Click the "Knit HTML" button! knitr will convert your R-Markdown into plain
Markdown, and then call Pandoc to complete the conversion into gloriously-tabbed

Tabs are very handy for reporting – but the whole HTML/CSS/JavaScript ecosystem
is at your disposal. If you've seen other good reporting tricks in HTML, let us
know in the comments below.

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)