-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTutorial6b_shiny.Rmd
More file actions
242 lines (206 loc) · 6.53 KB
/
Tutorial6b_shiny.Rmd
File metadata and controls
242 lines (206 loc) · 6.53 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
title: "R Notebook"
output:
html_document:
toc: true
toc_float: true
theme: spacelab
runtime: shiny
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
# Step 1: Getting ready
## Install `Shiny`.
```{r, eval=F}
install.packages("shiny")
```
## Load the libraries.
```{r}
library(shiny)
```
## Setting up your folder
R `shiny` provides a platform for making a web application from Rstudio. You need to select a working folder in your computer and set up an `app` folder, where you need to initiate two files: `ui.R` and `server.R`.

## Initiate app files
An example app with the pair of files:
```{r, echo=F}
library(shiny)
shinyApp(
ui=fluidPage(
sidebarPanel(
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)
),
mainPanel(
plotOutput("plot")
)
),
server<-function(input, output) {
output$plot=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")
})
}
)
```
### An example of `server.R`
```{r, eval=F}
library(shiny)
shinyServer(function(input, output) {
output$plot=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")
})
})
```
### An example of `ui.R`
```{r, eval=F}
shinyUI(
fluidPage(
sidebarPanel(
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)
),
mainPanel(
plotOutput("plot")
)
))
```
# Step 2: Use a Shiny theme
You can slightly change the look of your shiny app by choosing a [theme](https://rstudio.github.io/shinythemes/).
```{r, eval=F}
install.packages("shinythemes")
```
```{r, eval=F}
## ui.R ##
library(shinythemes)
fluidPage(theme = shinythemes("cerulean"),
...
)
```
```{r, echo=F}
library(shiny)
library(shinythemes)
shinyApp(
ui=fluidPage(theme = shinytheme("spacelab"),
sidebarPanel(
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)
),
mainPanel(
plotOutput("plot")
)
),
server<-function(input, output) {
output$plot=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")
})
}
)
```
# Step 3: Choose a data set to use
For the purpose of this exercise, we will use the `airquality` data set in R.
```{r}
library(shiny)
library(DT)
shinyApp(
ui=fluidPage(
headerPanel('Air Quality Data'),
sidebarPanel(
title = 'Air Quality Data',
selectInput('xcol', 'X Variable', names(airquality)[-c(5,6)],
selected=names(airquality)[[1]]),
selectInput('ycol', 'Y Variable', names(airquality)[-c(5,6)],
selected=names(airquality)[[2]]),
# actionButton('select2', 'Select the above variables.'),
sliderInput("subsample", label = "Size of random samples",
min = 5, max = 50, value = 10, step = 1),
actionButton('resetSelection',
label = "Click to reset row selection"
) # end of action button
),
mainPanel(
fluidRow(
column(6,
h1('select rows'),
DT::dataTableOutput('x1')),
column(6,
plotOutput('x2', height = 400))
),
verbatimTextOutput('info')
)
),
server<-function(input, output) {
selectedData <-
#airquality[,c(input$xcol,input$ycol)]
reactive({
airquality[, c(input$xcol, input$ycol)]
})
nn <- nrow(airquality)
output$x1 = DT::renderDataTable(airquality[,-c(5,6)],
options = list(
lengthMenu = list(c(3, 5, 10), c('3', '5', '10')),
pageLength = 5
),
server = FALSE,
selection = list(target = 'row+column'))
proxy = dataTableProxy('x1')
observeEvent(input$resetSelection, {
proxy %>% selectRows(sample(1:nn, input$subsample, replace=F))
})
# highlight selected rows in the scatterplot
output$x2 = renderPlot(height = 400, {
par(mar = c(4, 4, 1, .1))
plot(airquality[, c(input$xcol, input$ycol)])
s = input$x1_rows_selected
if (length(s)) {
points(airquality[s, c(input$xcol, input$ycol), drop = FALSE],
pch = 19, cex = 2)
abline(lsfit(airquality[s,input$xcol],
airquality[s,input$ycol])$coef, col=2)
}
})
output$info = renderPrint({
s = input$x1_rows_selected
cor.sel=NA
if(length(s)) cor.sel=cor(airquality[s,input$xcol],
airquality[s,input$ycol],
use="pairwise.complete.obs")
list(xcol=input$xcol, ycol=input$ycol,
cor.all=cor(airquality[,input$xcol],
airquality[,input$ycol],
use="pairwise.complete.obs"),
cor.sel=cor.sel)
})
}
)
```
# Step 4: make a R flex dashboard
```{r, eval=F}
install.packages("flexdashboard", type = "source")
```
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Cmd+Shift+K* to preview the HTML file).