R Markdown is one of my favorite things about modern R. It offers an easy way to combine text, R code, and the results of R code in a single document. And when that document is rendered as HTML, you can add some user interaction with HTML widgets like DT for tables or leaflet for maps. (If you’re not familiar with R Markdown, you can check out my first and then come back here.)
But you may not know that there’s a way to amp up R Markdown interactivity even more: by adding
runtime: shiny to the document header.
Shiny is a Web application framework for R. As a framework, it has a fairly specific structure. However, you can convert an R Markdown document into a Shiny app without having to follow a lot of that rigid structure. Instead, you can jump right in and start coding—without worrying about some typical Shiny tasks like ensuring all your parentheses and commas are correct within deeply nested layout functions.
In fact, even if you’re an experienced shiny developer, an R Markdown document can still be useful for shiny tasks where you don’t need a full-blown application or for quickly trying out code. It will still need a Shiny server, but if you’ve got RStudio and the shiny package installed, you already have one of those locally.
Let’s take a look at how runtime shiny works in R Markdown.