Interactive R Markdown with Shiny
Stefan Daume
16. December 2022
SRC PhD R course Module 7
Interactive R Markdown with Shiny
Stefan Daume
Stockholm Resilience Centre, Stockholm University
& Beijer Institute of Ecological Economics
16. December 2022
Allows to create interactive web applications running R.
Requires three components:
In order to run this a web server is required, a basic server is built into R/RStudio
or apps can be deployed to hosted R Servers like shinyapps.io
library(shiny)
ui <- fluidPage(
# defines layout and web page components
)
server <- function(input, output) {
# defines the R logic and maps web page
# inputs to rendered outputs
}
shinyApp(ui = ui, server = server)
library(shiny)
ui <- fluidPage(
selectInput(inputId = "selectedDataset", label = "Dataset",
choices = ls("package:datasets")),
tableOutput(outputId = "myTable")
)
server <- function(input, output) {
output$myTable <- renderTable({
dataset <- get(input$selectedDataset, "package:datasets")
dataset
})
}
shinyApp(ui = ui, server = server)
Test it here: https://scitingly.shinyapps.io/datasets/
If you are already using RMarkdown for writing your papers or to document your analyses, then it is easy to turn these documents into interactive web applications.
flexdashboardsshiny and deployed as a web applicationMarkdown allows us to concentrate on document structure and content. We can then worry about styling and presentation later.
knitr package, an R package conveniently integrated into the R Studio UI.Basic (R)Markdown
# Top-level header
## Second-level header
This is a list:
* with some **bold** and
* some *italic* text.
And a [hyperlink](https://bookdown.org/yihui/rmarkdown/) for good measure.
Typical workflow with markdown:

knit the document.The YAML header must be placed at the beginning of a document and is enclosed by three dashes ---.
---
title: "Untitled"
output: html_document
date: '2022-12-16'
---
Above is the default YAML header when generating an RMarkdown file in R Studio.
The RMarkdown document is knit to the output format specified in the YAML header.

---
title: "Untitled"
output: html_document
date: '2022-12-16'
runtime: shiny
---
The output format is now a web application.

Which will be included in the code chunks of the R Markdown document.
In RStudio select: File > New File > R Markdown

Default example
```{r eruptions, echo=FALSE}
inputPanel(
selectInput("n_breaks", label = "Number of bins:",
choices = c(10, 20, 35, 50), selected = 20),
sliderInput("bw_adjust", label = "Bandwidth adjustment:",
min = 0.2, max = 2, value = 1, step = 0.2)
)
renderPlot({
hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks),
xlab = "Duration (minutes)", main = "Geyser eruption duration")
dens <- density(faithful$eruptions, adjust = input$bw_adjust)
lines(dens, col = "blue")
})
```
Dashboards are composed of rows and columns. Each output component is indicated by a level 3 header (i.e. ###).
flex_dashboard layout to vertical---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
vertical_layout: scroll
date: "2022-12-16"
runtime: shiny
---
flexdashboard allows flexible layouts that are basically controlled through markdown section headers at three levels.
# and =========================== will work.Place the “Inputs” and “Outputs” sections next to each other. This can be achieved by adding a level 2 markdown section header named Column above each of these sections. Both the header variant ## and ----------------------- will work.
flexdashboard offers flexible styling of the output. Several built-in themes can be applied via the YAML header.
There is a broad range of styling options and components that can be controlled via the YAML header and standard R Markdown elements.
Bryan, Jennifer. 2017. “Excuse me, do you have a moment to talk about version control?” PeerJ Preprints 5:e3159v2 (August). https://doi.org/10.7287/PEERJ.PREPRINTS.3159V2.
———. 2021. “Happy Git and GitHub for the useR.” https://happygitwithr.com/.
Chacon, Scott, and Ben Straub. 2014. Pro Git. Apress. https://doi.org/10.1007/978-1-4842-0076-6.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2022. “R Markdown: The Definitive Guide.” https://bookdown.org/yihui/rmarkdown/.
SRC PhD R course Module 7 — Interactive R Markdown with Shiny" by Stefan Daume
Presented on 16. December 2022.
This presentation can be cited using: doi:…
PRESENTATION DETAILS
Author/Affiliation: Stefan Daume, Stockholm Resilience Centre, Stockholm University
Presentation URL: https://sdaume.github.io/r-course-module-3/slides/shiny-module.html
Presentation Source: [TBD]
Presentation PDF: [TBD]
CREDITS & LICENSES
This presentation is delivered with the help of several free and open source tools and libraries. It utilises the reveal.js presentation framework and has been created using RMarkdown, knitr, RStudio and Pandoc. highlight.js provides syntax highlighting for code sections. MathJax supports the rendering of mathematical notations. PDF and JPG copies of this presentation were generated with DeckTape. Please note the respective licenses of these tools and libraries.
If not noted and attributed otherwise, the contents (text, charts, images) of this presentation are Copyright © 2022 of the Author and provided under a CC BY 4.0 public domain license.