-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathChapter3.Rmd
More file actions
170 lines (130 loc) · 4.73 KB
/
Copy pathChapter3.Rmd
File metadata and controls
170 lines (130 loc) · 4.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: "Chapter2"
author: "Maya Gans"
date: "3/29/2020"
output: html_document
css: styles.css
---
## 3.2.8.1
When space is at a premium, it’s useful to label text boxes using a placeholder that appears inside the text entry area. How do you call `textInput()` to generate the UI below?
```{r eval=FALSE}
textInput("text", "Text", placeholder = "Your name")
```
## 3.2.8.2
Carefully read the documentation for `sliderInput()` to figure out how to create a date slider
```{r eval=FALSE}
sliderInput("dates", "Date Range:",
min = lubridate::as_date("2020-01-01"),
max = lubridate::as_date("2020-12-31"),
value = lubridate::as_date("2020-03-26"))
)
```
## 3.2.8.3
If you have a moderately long list, it’s useful to create sub-headings that break the list up into pieces. Read the documentation for `selectInput()` to figure out how. (Hint: the underlying HTML is called <optgroup>.)
```{r, eval=FALSE}
selectInput("state", "Choose a number:",
list(`ONE` = list(1,2,3),
`TWO` = list(4,5,6),
`THREE` = list(7,8,9))
)
```
We can make the `choices` argument a list, where the header is followed by list items that can selected from.
## 3.2.8.4
Create a slider input to select values between 0 and 100 where the interval between each selectable value on the slider is 5. Then, add animation to the input widget so when the user presses play the input widget scrolls through automatically.
We can increment the value using the `step` argument, and play thought automatically by setting `animate = TRUE`
```{r, eval=FALSE}
sliderInput("slider", "Select Number",
min = 0, max = 100, value = 0,
step = 5, animate = TRUE)
```
## 3.2.8.5
Using the following numeric input box the user can enter any value between 0 and 1000. What is the purpose of the step argument in this widget?
```{r, eval=FALSE}
numericInput("number", "Select a value", value = 150, min = 0, max = 1000, step = 50)
```
When the user clicks either of the [Spin Boxes](https://en.wikipedia.org/wiki/Spinner_%28computing%29), the number is incremented by +/- 50. However, by using a `numericInput` the user still has the ability to select *any* number.
## 3.3.5.1
Re-create the Shiny app from the plots section, this time setting height to 300px and width to 700px.
```{r eval=FALSE}
ui <- fluidPage(
plotOutput("plot", width = "700px", height="300px")
)
server <- function(input, output, session) {
output$plot <- renderPlot(plot(1:5))
}
shinyApp(ui, server)
```
## 3.3.5.2
Add an additional plot to the right of the existing plot, and size it so that each plot takes up half the width of the app.
```{r, eval=FALSE}
ui <- fluidPage(
fluidRow(
column(6, plotOutput("plot")),
column(6, plotOutput("plot2"))
)
)
server <- function(input, output, session) {
output$plot <- renderPlot(plot(1:5))
output$plot2 <- renderPlot(plot(1:5))
}
shinyApp(ui, server)
```
## 3.3.5.3
Update the options for `renderDataTable()` below so that the table is displayed, but nothing else, i.e. remove the search, ordering, and filtering commands. You’ll need to read ?renderDataTable and review the options at https://datatables.net/reference/option/.
We can achieve this by setting `ordering` and `searching` to `FALSE` within the `options` list.
```{r eval=FALSE}
library(shinythemes)
ui <- fluidPage(
dataTableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderDataTable(mtcars, options = list(ordering = FALSE, searching = FALSE))
}
shinyApp(ui, server)
```
## 3.4.6.1
Modify the Central Limit Theorem app so that the sidebar is on the right instead of the left.
```{r, eval=FALSE}
ui <- fluidPage(
headerPanel("Central limit theorem"),
sidebarLayout(position = "right",
sidebarPanel(
numericInput("m", "Number of samples:", 2, min = 1, max = 100)
),
mainPanel(
plotOutput("hist")
)
)
)
server <- function(input, output, session) {
output$hist <- renderPlot({
means <- replicate(1e4, mean(runif(input$m)))
hist(means, breaks = 20)
})
}
shinyApp(ui, server)
```
## 3.4.6.2
Browse the themes available in the shinythemes package, pick an attractive theme, and apply it the Central Limit Theorem app.
```{r, eval=FALSE}
library(shinythemes)
ui <- fluidPage(
theme = shinythemes::shinytheme("darkly"),
headerPanel("Central limit theorem"),
sidebarLayout(position = "right",
sidebarPanel(
numericInput("m", "Number of samples:", 2, min = 1, max = 100)
),
mainPanel(
plotOutput("hist")
)
)
)
server <- function(input, output, session) {
output$hist <- renderPlot({
means <- replicate(1e4, mean(runif(input$m)))
hist(means, breaks = 20)
})
}
shinyApp(ui, server)
```