Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created April 24, 2014 10:57
Show Gist options
  • Save jjallaire/9390e6dfab153759423f to your computer and use it in GitHub Desktop.
Save jjallaire/9390e6dfab153759423f to your computer and use it in GitHub Desktop.
shiny pandoc crash
---
title: "Shiny Document"
output: html_document
runtime: shiny
---
This R Markdown document is made interactive using Shiny (for more information on Shiny see <http://shiny.rstudio.com>). Note that R Markdown documents using Shiny can currently only be run locally (support for running them within Shiny Server will be available soon).
## Inputs and outputs
You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. In this example we create a `numericInput` with the name "rows" and then refer to it's value via `input$rows` when generating output:
```{r, echo=FALSE}
numericInput("rows", "How many cars?", 5)
renderTable({
head(cars, input$rows)
})
```
In this example the output code was wrapped in a call to `renderTable`. There are many other render functions in Shiny that can be used for plots, printed R output, and more. This example uses `renderPlot` to create dynamic plot output:
```{r, echo=FALSE}
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30)
renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
```
## Embedded applications
It's also possible to embed an entire Shiny application within an R Markdown document. There are two syntaxes for this:
1) Defining the application inline using the `shinyApp` function; or
2) Referring to an external application directory using the `shinyAppDir` function.
This example uses an inline definition:
```{r, echo=FALSE}
shinyApp(
ui = fluidPage(
selectInput("region", "Region:",
choices = colnames(WorldPhones)),
plotOutput("phonePlot")
),
server = function(input, output) {
output$phonePlot <- renderPlot({
barplot(WorldPhones[,input$region]*1000,
ylab = "Number of Telephones", xlab = "Year")
})
},
options = list(height = 500)
)
```
Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy.
This example embeds a Shiny application defined in another directory:
```{r, echo=FALSE}
shinyAppDir(
system.file("examples/06_tabsets", package="shiny"),
options=list(
width="100%", height=550
)
)
```
Note that in all of R code chunks above the `echo = FALSE` attribute is used. This is to prevent the R code within the chunk from rendering in the document alongside the Shiny components.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment