-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
354 lines (289 loc) · 14.6 KB
/
index.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
---
title: "Catnip QC Shiny App"
author: "Bo Yuan"
date: "2/2/2020"
output: html_document
---
```{r, eval=FALSE}
library(shiny)
library(readxl)
library(rebus)
library(writexl)
library(rdrop2)
library(DT)
library(tidyverse)
library(RColorBrewer)
library(ggrepel)
# Import data from Dropbox ----
drop_auth()
token = drop_auth()
saveRDS(token, file = "token.rds")
theme_set(theme_bw() +
theme(strip.background = element_blank(),
strip.text = element_text(face = "bold", colour = "black", size = 12),
axis.text = element_text(colour = "black", size = 11),
axis.title = element_text(size = 14),
title = element_text(face = "bold"))
)
# Define core functions ----
# Barplot making
# UI ----
ui <- fluidPage(
# Application title
titlePanel(strong("Catnip Quality Control")),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
### Dataset
h4(strong(em("Dataset Input"))),
# template download
downloadButton("downloadTemplate", "Download quality control template",
style="color: white; background-color: steelblue; border-color: black"),
# user upload datset
fileInput(inputId = "userfile", label = NULL, buttonLabel = "Browse...Excel input",
width = '700px', placeholder = "Upload catnip quality control dataset"),
### Plot control
h4(strong(em("Global Plot Control"))),
fluidRow(
column(6,
sliderInput(inputId = "pltHeight", label = "Plot height",
min = 100, max = 2000, step = 50, value = 600),
sliderInput(inputId = "pltWidth", label = "Plot Width",
min = 100, max = 2000, step = 50, value = 900)),
# column(1),
column(6,
radioButtons(inputId = "eachCmpd", label = "Show compounds",
choices = c("Individual" = T, "Total" = F),
selected = T, inline = T),
selectInput(inputId = "plotColor", label = "Colors",
choices = c("Rainbow" = 1, "Accent" = "Accent", "Dark2" = "Dark2", "Paired" = "Paired", "Pastel1" = "Pastel1", "Pastel2" = "Pastel2", "Set1" = "Set1", "Set2" = "Set2", "Set3" = "Set3", "Gradient greys" = "Greys", "Gradient blues" = "Blues", "Gradient greens" = "Greens"),
selected = 1),
sliderInput(inputId = "facetRowNumber", label = "Display row numbers",
min = 1, max = 6, value = 2)
)
),
strong(em("Bar plot"), style="color: darkred"),
# Format
fluidRow(
# doge, stack vs. fill
column(7, radioButtons(inputId = "barPosition", label = "Compounds display format",
choices = c("Dodge" = "dodge", "Stack" = "stack", "Fill" = "fill"),
selected = "dodge", inline = T)),
# Orientation?
column(4, strong("Flip plot"), checkboxInput(inputId = "FlipPlot", label = "", value = T))
),
strong(em("Box plot", style = "color: darkred")),
fluidRow(
column(6, sliderInput(inputId = "bubbleSize", label = "Bubble size",
min = .5, max = 8, step = .25, value = 3)),
column(1),
column(4, strong("Show names"), checkboxInput(inputId = "showText", label = "", value = F))
),
h4(strong(em("Content table"))),
# show standard deviation or not (show sd not allowing for numeric assort)
fluidRow(
column(6, checkboxInput(inputId = "DTstd", label = "Show standard deviation", value = F)),
column(6, downloadButton("downloadContent", "Save content table",
style="color: white; background-color: orange; border-color: black"))
)
),
# Show a plot of the generated distribution # ----
mainPanel(
tabsetPanel(
tabPanel("Bar plot", br(), uiOutput("ui.barplot")),
tabPanel("Box plot", br(), uiOutput("ui.boxplot")),
tabPanel("Content table",br(), dataTableOutput("contentTable"))
)
)
)
)
# SERVER
server <- function(input, output) {
# read template from my dropbox
df.CatnipQCtemplate = drop_read_csv(file = "catnip Quant December 2019.csv")
output$downloadTemplate = downloadHandler(
filename = "Catnip QC Template.xlsx",
content = function(file) {
write_xlsx(df.CatnipQCtemplate, path = file)
}
)
# bar color specification
colorSet = c("Accent", "Dark2", "Paired", "Pastel1", "Pastel2", "Set1", "Set2", "Set3",
"Greys", "Blues", "Greens")
max.colorSet = c(8, 8, 12, 9, 8, 9, 8, 12, rep(9, 3))
names(max.colorSet) = colorSet # maximum color choice number within each color palette set
# Dataset preparation: user input vs. default dataset-<>--<>--<>--<>--<>--<>--<>--<>-
simpleStats = reactive({ # EVEN IF NO INPUT FILE, THIS REACTIVE SCRIPT STILL RUNS...!! A CONVENINET FEATURE TO REMEMBER
if(is.null(input$userfile)) { d = df.CatnipQCtemplate # if no userinput file, use template
} else {
infile = input$userfile # update d with user input dataset
d = read_excel(infile$datapath) # d being the original dataset
}
d.summary =
d %>% group_by(Groups) %>%
gather(contains("NA"), contains("NT"), contains("DHNL"), contains("NL"), contains("total"),
key = compounds, value = content) %>%
group_by(Groups, compounds) %>%
summarise(content.mean = mean(content),
content.sd = sd(content)) # d.summary being the original summary stats dataset
#Feb 2 2020 notes:
if(input$eachCmpd == T) { # plot individual compounds
d.summary.cmpdSelected = d.summary %>% filter(!compounds %>% str_detect("total"))
} else{ # plot subtotal of each category of compounds
d.summary.cmpdSelected = d.summary %>% filter(compounds %>% str_detect("total"))
}
return(list(d, d.summary, d.summary.cmpdSelected)) # datatable shows always individual compounds and the total
})
d = reactive({ simpleStats()[[1]] })
d.summary = reactive({ simpleStats()[[2]] })
d.summary.cmpdSelected = reactive({ simpleStats()[[3]] })
# bar plot
func.plt.contentBar = function(myDataset, FlipPlot = T,
barPosition = "dodge" , plotColor = 1, bar.rowNumber) {
dataset = myDataset
if(barPosition %in% c("stack", "fill")) {
# do not stack the final total content for stacked bars
# either stack subtotal of individual compounds depend on argument "eachCmpd"
dataset = dataset %>% filter(compounds != "total")
}
# basc plot
plt = dataset %>%
ggplot(aes(x = Groups, y = content.mean)) +
labs(title = "Compounds content in catnip (mg/100 g dry weight)")
# Barplot position
if (barPosition == "dodge") { # add error bar
plt = plt +
geom_bar(stat = "identity", aes(fill = Groups), # when dodged, color with groups
alpha = .7, width = .9) +
geom_errorbar(aes(ymin = content.mean - content.sd,
ymax = content.mean + content.sd,
color = Groups),
width = 0.5) +
theme(legend.position = "None") # no legend for groups annotation
} else if (barPosition == "stack") {
plt = plt +
geom_bar(stat = "identity", aes(fill = compounds), # when stack or filled, color with compounds
position = "stack", alpha = .9, width = .9)
} else if (barPosition == "fill") {
plt = plt +
geom_bar(stat = "identity", aes(fill = compounds),
position = "fill", alpha = .9, width = .9)
}
# flip plot?
if(FlipPlot == T) {
plt = plt + coord_flip()
if (barPosition == "dodge") { # if fill or stack, no need to facet regarding compounds
plt = plt + facet_wrap(~compounds, scales = "free_x", nrow = bar.rowNumber)
# group axis vertical, free the content x-axis }
}
} else { # no flip, i.e., group on the horizontal axis
plt = plt +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
if (barPosition == "dodge") { # if fill or stack, no need to facet regarding compounds
plt = plt + facet_wrap(~compounds, scales = "free_y", nrow = bar.rowNumber)
# group axis horizontal, free content y-axis
}
}
# Color by compounds
if(barPosition %in% c("stack", "fill") & plotColor != 1) {
n.cmpds = dataset$compounds %>% n_distinct()
if(n.cmpds > max.colorSet[plotColor]) {
color.cmpds = colorRampPalette(brewer.pal(max.colorSet[plotColor], plotColor))(n.cmpds)
plt = plt + scale_color_manual(values = color.cmpds) +
scale_fill_manual(values = color.cmpds)
} else {
plt = plt + scale_color_brewer(palette = plotColor) + scale_fill_brewer(palette = plotColor)
}
}
# Color by groups
if(barPosition == "dodge" & plotColor != 1) {
n.groups = dataset$Groups %>% n_distinct() # group number
if(n.groups > max.colorSet[plotColor]) { # more finely divided color steps
color.groups = colorRampPalette(brewer.pal(max.colorSet[plotColor], plotColor))(n.groups)
plt = plt + scale_color_manual(values = color.groups) +
scale_fill_manual(values = color.groups)
} else { plt = plt + scale_color_brewer(palette = plotColor) + scale_fill_brewer(palette = plotColor) }
}
return(plt)
}
barplotContent = reactive({
d.summary.cmpdSelected() %>%
func.plt.contentBar(input$FlipPlot, barPosition = input$barPosition, input$plotColor,
bar.rowNumber = input$facetRowNumber)
})
output$barplotContent = renderPlot({ barplotContent() }) # summary bar plot
output$ui.barplot = renderUI({
plotOutput("barplotContent", height = input$pltHeight, width = input$pltWidth)
})
# Box plot -<>--<>--<>--<>--<>--<>--<>--<>--<>--<>--<>-
func.plt.box = function(dataset, box.facetRowNumber, plotColor = 1, bubbleSize, showText = F){
set.seed(2020) # do not change bubble point position when update point size
plt = dataset %>%
ggplot(aes(x = 1, y = content.mean, fill = compounds, color = compounds)) +
geom_boxplot(alpha = .2, outlier.alpha = 0) +
geom_point(position = position_jitter(.2),
shape = 21, fill = "white", size = bubbleSize) +
facet_wrap(~ compounds, scales = "free_y", nrow = box.facetRowNumber) +
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank()) +
labs(y = "Averaged content (mg/100g dry mass)",
title = "Compound content distribution boxplot")
# specify color
if (plotColor !=1 ){ # 1 being default value without uses' color input
n.cmpds = dataset$compounds %>% n_distinct()
if(n.cmpds > max.colorSet[plotColor]) {
color.cmpds = colorRampPalette(brewer.pal(max.colorSet[plotColor], plotColor))(n.cmpds)
plt = plt + scale_color_manual(values = color.cmpds) +
scale_fill_manual(values = color.cmpds)
} else {
plt = plt + scale_color_brewer(palette = plotColor) + scale_fill_brewer(palette = plotColor)
}
}
if (showText == T) {
plt = plt + geom_text_repel(aes(label = Groups))
}
return(plt)
}
output$boxplotContent = renderPlot({
d.summary.cmpdSelected() %>%
func.plt.box(box.facetRowNumber = input$facetRowNumber,
plotColor = input$plotColor, bubbleSize = input$bubbleSize, showText = input$showText)
})
output$ui.boxplot = renderUI({
plotOutput("boxplotContent", height = input$pltHeight, width = input$pltWidth)
})
# output summary table -<>--<>--<>--<>--<>--<>--<>--<>--<>--<>--<>--<>-
# Define function combine mean with standard deviation
func.d.summary.wrapUp = function(d.summary, showSD = F){
if (showSD == F) {
d.summary = d.summary %>%
select(Groups, compounds, content.mean) %>%
mutate(content.mean = round(content.mean, 2)) %>%
spread(key = compounds, value = content.mean)
} else { # output both average and standard deviation
d.summary = d.summary %>%
mutate(content = paste(round(content.mean, 2), "±", round(content.sd, 2)) ) %>%
select(Groups, compounds, content) %>%
spread(key = compounds, value = content)
}
return(d.summary)
}
# combine average with sd
d.summaryDToutput = reactive({ d.summary() %>% func.d.summary.wrapUp(showSD = input$DTstd) })
output$contentTable = renderDataTable({ d.summaryDToutput() })
# save content table
output$downloadContent = downloadHandler(
filename = "Catnip content.xlsx",
content = function(file) {
write_xlsx(d.summaryDToutput(),
# whatever online table output, always show up mean, sd and mean with sd when saved
path = file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
# ----------
```