Dropdown menus in R Markdown with bsselectR
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I strongly believe that interactive reports, presentations, and scholarly articles are going to become much more prominent in the years ahead. Whereas a PDF article or presentation can often only show a limited aspect of a research project, interactive documents can allow a reader or presenter to explore project content in a much broader sense.
For dynamic research documents, an excellent option is to combine Shiny with R Markdown to generate a report that can execute R code from a Shiny server. However, not all interactive documents need the full power of Shiny. For example, I’m currently writing a report in which I’d like readers to be able to use drop-down menus to select charts to view. As I produced the charts in Python and saved them all to a folder, I don’t need Shiny to dynamically create the charts in the report.
To create the drop-down menu, I came across the bootstrap-select jQuery plugin. bootstrap-select adds Bootstrap’s dropdown.js and Bootstrap styling to re-style HTML menus, which makes it a nice fit for R Markdown documents that default to this styling. While writing out the raw HTML and JavaScript to accomplish this worked in R Markdown, I found this tedious, so I decided to author bsselectR, an htmlwidget to do this automatically with R code. Many, many thanks are due to the developers of bootstrap-select for their hard work and excellent documentation.
bsselectR is not on CRAN, but you can install the development version from GitHub with devtools::install_github("walkerke/bsselectR")
. View the source code at https://github.com/walkerke/bsselectR.
At the moment, there is one main function in bsselectR, bsselect
, which will generate a bootstrap-select dropdown menu that allows you to interactively display text, images, or HTML documents as iframes. Pass a named vector of text or paths to images/HTML documents and specify the type as "text"
, "img"
, or "iframe"
, and bsselect
will return a drop-down menu. On a basic level, bsselect
can work outside of R Markdown, using the default Bootst