R Markdown’s new interactive documents provide a quick, light-weight way to use Shiny. An interactive document embeds Shiny elements in an R Markdown report. The report becomes “live”, a choose your own adventure that readers can control and explore. Interactive documents are easy to create and easy to share.
To create an interactive document use RStudio to create a new R Markdown file, choose the Shiny document template, then click “Run Document” to show a preview:
Include Shiny widgets and outputs in your code chunks. R Markdown will insert the widgets directly into your final document. When a reader toggles a widget, the parts of the document that depend on it will update instantly.
That’s it! No extra files are needed.
Note that in order to use interactive documents you should be running the latest version of RStudio (v0.98.932 or higher). Alternatively if you are not using RStudio be sure to follow the directions here to install all of the required components.
Interactive documents can be run locally on the desktop or be deployed Shiny Server v1.2 or ShinyApps just like any other Shiny application. See the RMarkdown v2 website for more details on deploying interactive documents.
Interactive documents make it easy to insert powerful tools into a report. For example, you can insert a kmeans clustering tool into your document with one line of code, as below.
kmeans_cluster is a widget built from a Shiny app and intended for use in interactive documents.
You can build your own widgets with
shinyApp, a new function that repackages Shiny apps as functions.
shinyApp is easy to use. Its first argument takes the code that appears in an app’s ui.R file. The second argument takes the code that appears in the app’s server.R file. The source of
kmeans_cluster reveals how simple this is.
Ready to be a hero? You can use the
shinyApp function to make out of the box widgets that students, teachers, and data scientists will use everyday. Widgets can
demonstrate teaching examples
act as quizzes or multiple choice questions
These widgets are not made yet, they are low hanging fruit for any Shiny developer. If you know how to program with Shiny (or want to learn), and would like to make your mark on R, consider authoring a package that makes widgets available for interactive documents.
To learn more about interactive documents visit https://rmarkdown.rstudio.com/authoring_shiny.html.
In this series, we walk through lesser-known tips and tricks to help you work more effectively and efficiently in R Markdown. This third post focuses on features that save you time and trouble.
The RStudio IDE includes a visual markdown editor that displays changes in real-time and provides support for technical writing. This post walks through several features of visual editing mode.