-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractive_RMarkdown_Example.Rmd
More file actions
112 lines (82 loc) · 3.4 KB
/
Copy pathInteractive_RMarkdown_Example.Rmd
File metadata and controls
112 lines (82 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
title: "Interactive RMarkdown Example"
author: "Dustin Pluta"
date: "May 11, 2017"
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately.
To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).
## Inputs and Outputs
You can embed Shiny inputs and outputs in your document. Outputs are automatically updated whenever inputs change. This demonstrates how a standard R plot can be made interactive by wrapping it in the Shiny `renderPlot` function. The `selectInput` and `sliderInput` functions create the input widgets used to drive the plot.
```{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")
})
```
## Embedded Application
It's also possible to embed an entire Shiny application within an R Markdown document using the `shinyAppDir` function. This example embeds a Shiny application located in another directory:
```{r tabsets, echo=FALSE}
shinyAppDir(
system.file("examples/06_tabsets", package = "shiny"),
options = list(
width = "100%", height = 550
)
)
```
Note the use of the `height` parameter to determine how much vertical space the embedded application should occupy.
You can also use the `shinyApp` function to define an application inline rather then in an external directory.
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.
## Examples of Widgets
### [Widget Gallery](https://shiny.rstudio.com/gallery/widget-gallery.html)
### Widgets in Action
##### Action Button
```{r, echo = FALSE}
actionButton("action", label = "Action")
renderPrint({ input$action })
```
##### Checkbox Group
```{r, echo = FALSE}
checkboxGroupInput("checkGroup", label = h3("Checkbox group"),
choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
selected = 1)
renderPrint({ input$checkGroup })
```
##### Slider
```{r, echo = FALSE}
sliderInput("slider1", label = h3("Slider"), min = 0,
max = 100, value = 50)
sliderInput("slider2", label = h3("Slider Range"), min = 0,
max = 100, value = c(40, 60))
renderPrint({ input$slider1 })
renderPrint({ input$slider2 })
```
##### Select Box
```{r, echo = FALSE}
selectInput("select", label = h3("Select box"),
choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
selected = 1)
renderPrint({ input$select })
```
##### Text Input
```{r, echo = FALSE}
textInput("text", label = h3("Text input"), value = "Enter text...")
renderPrint({ input$text })
```
##### Numeric Input
```{r, echo = FALSE}
numericInput("num", label = h3("Numeric input"), value = 1)
renderPrint({ input$num })
```