I was having a wander through R Markdown: The Definitive Guide, and I found an interesting example: embedding Shiny app in an R Markdown document.
---
title: "A Shiny Document"
output: html_document
runtime: shiny
---
A standard R plot can be made interactive by wrapping
it in the Shiny `renderPlot()` function. The `selectInput()`
function creates the input widget to drive the plot.
```{r eruptions, echo=FALSE}
selectInput(
'breaks', label = 'Number of bins:',
choices = c(10, 20, 35, 50), selected = 20
)
renderPlot({
par(mar = c(4, 4, .1, .5))
hist(
faithful$eruptions, as.numeric(input$breaks),
col = 'gray', border = 'white',
xlab = 'Duration (minutes)', main = ''
)
})
I understand adding runtime: shiny
to the YAML, but I was surprised by the chunk content. Usually in Shiny apps, I would expect to see selectInput()
given to a layout function like fluidPage()
, and renderPlot()
would be called inside a server function, and then you'd call shiny()
with the layout function call and the server function given to that.
Here, selectInput()
and renderPlot()
are thrown together in the chunk. Is it simply that the rest of the boilerplate isn't required in simple cases, or is R Markdown or knitr doing something behind the scenes to ensure these two functions are hooked up properly?