-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path16-escaping-graph.Rmd
100 lines (79 loc) · 1.77 KB
/
16-escaping-graph.Rmd
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
# Escaping the graph
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
## 16.3.4 Exercises {-}
1.
```{r}
library(shiny)
ui <- fluidPage(
actionButton("rnorm", "Normal"),
actionButton("runif", "Uniform"),
plotOutput("plot")
)
server <- function(input, output, session) {
r <- reactiveValues(random_data = vector(mode = "numeric", length = 100))
observeEvent(input$rnorm, {
r$random_data <- rnorm(100)
})
observeEvent(input$runif, {
r$random_data <- runif(100)
})
output$plot <- renderPlot({
# Only show plot if input$rnorm
# OR input$runif is provided
req(input$rnorm | input$runif)
hist(r$random_data)
})
}
shinyApp(ui, server)
```
2.
```{r}
library(shiny)
ui <- fluidPage(
selectInput("type", "type", c("Normal", "Uniform")),
actionButton("go", "go"),
plotOutput("plot")
)
server <- function(input, output, session) {
r <- reactiveValues(random_data = vector(mode = "numeric", length = 100))
observeEvent(input$go, {
if (input$type == "Normal") {
r$random_data <- rnorm(100)
} else {
r$random_data <- runif(100)
}
})
output$plot <- renderPlot({
# Only show plot if "go" is clicked
req(input$go)
hist(r$random_data)
})
}
shinyApp(ui, server)
```
3.
```{r}
library(shiny)
ui <- fluidPage(
selectInput("type", "type", c("Normal", "Uniform")),
actionButton("go", "go"),
plotOutput("plot")
)
server <- function(input, output, session) {
r <- reactive({
if (input$type == "Normal") {
rnorm(100)
} else if (input$type == "Uniform") {
runif(100)
}
})
output$plot <- renderPlot({
req(input$go)
hist(r())
})
}
shinyApp(ui, server)
```
- You can do that for the second UI but not the first because the second UI has the `go` actionButton.