/
metaboapp.R
380 lines (344 loc) · 12.9 KB
/
metaboapp.R
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
pkgs = as.character(installed.packages()[,'Package'])
req.pkgs = c('shiny', 'shinyFiles', 'shinymanager', 'readr', 'tidyverse',
'DT', 'purrr', 'magick', 'stringr')
sapply(req.pkgs, function(x) { if(!(x %in% pkgs)) install.packages(x) })
library(shiny)
library(shinyFiles)
library(shinymanager)
library(readr)
library(tidyverse)
library(DT)
library(purrr)
library(magick)
library(stringr)
search_settings = list()
file_list = list()
rdata_set_cumulate = data.frame()
rdata_set_select = data.frame()
rdata_select_prepared = data.frame()
rdata_set = data.frame()
xz = c()
inactivity <- "function idleTimer() {
var t = setTimeout(logout, 120000);
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
function logout() {
window.close(); //close the window
}
function resetTimer() {
clearTimeout(t);
t = setTimeout(logout, 120000); // time is in milliseconds (1000 is 1 second)
}
}
idleTimer();"
credentials = data.frame(
user = c("1"),
password = c("1"),
stringsAsFactors = FALSE
)
ui = #secure_app(head_auth = tags$script(inactivity),
fluidPage(
fluidRow(
headerPanel(h3("MetaboView: metabolite results viewer", style="color: #02169B; font-weight: bold;")),
div(style = "height:72px; background-color: #F1F1F1;")
),
br(),
fluidRow(
column(width = 7,
wellPanel(
h3(tags$b("Instructions")),
hr(),
h4("This app works sequentially; stages have to be followed in the order provided. If you would like to start over,
restart the app. If you are searching and sorting on a set of variables and would like to change options, always
return to Step 3."),
h4(""),
h4("")
)
)
),
br(),
sidebarPanel(width = 4,
wellPanel(
h4("Step 1: "),
h5("Select the CSV plot attributes data set from your files."),
fileInput("data_set", "Choose CSV data set",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
),
wellPanel(
h4("Step 2: "),
h5("Select the directy to the plot jpeg files. Use the black arrows in the left window to navigate your file system,
and then select the final directory with your curser (it should highligh blue). The right window should display
the list of plots."),
shinyDirButton('dir', label='File select', title='Select Metabolite image directory'),
textOutput("mdat_path_display")
),
wellPanel(
h4("Step 3: "),
h5("Click 'View options' to display variables which you can dynamically explore. Then, make your selections using the
check boxes."),
actionButton('view_options', 'View options'),
h5(''),
h5(''),
checkboxGroupInput('select_vars', label = 'Select variables on which to explore metabolites',
choices = c()),
actionButton('update_select', 'Update selected variable')
),
wellPanel(
h4("Step 4: "),
h5("Select all options you would like to use in the sorting, filtering, and searching process."),
checkboxGroupInput('select_sorting_options', label = 'Select sorting options',
choices = c()),
actionButton('update_select_w_options', 'Update with options')
),
wellPanel(
h4("Step 5: "),
h5("Click 'Run' to generate a new set of results:"),
uiOutput('sidebar_to_explore2'),
actionButton('run', 'Run')
),
wellPanel(
h4('Extra: '),
h5("You can print the complete data set to sort and search: "),
actionButton('data_preview', 'Preview attributes data')
)
),
mainPanel(width = 8,
dataTableOutput('contents'),
uiOutput('imageUI')
)
)#)
server = function(input, output, session) {
result_auth = secure_server(check_credentials = check_credentials(credentials))
volumes <- getVolumes()
shinyDirChoose(
input,
'dir',
roots = volumes(),
session = session,
filetypes = c('', 'jpg')
)
mdat_path <- reactive({
return(parseDirPath(volumes, input$dir))
})
output$mdat_path_display = renderText({
req(input$dir)
dir_ = unlist(input$dir)
dir2_ = dir_[-length(dir_)]
dir_start_ = dir_[length(dir_)]
dir_new_ = c(dir_start_, dir2_)
return(paste0(dir_new_, sep = '/'))
})
observeEvent(input$data_set, {
rdata_set <<- read_csv(input$data_set$datapath)
})
output$contents =
renderDataTable({
req(rdata_set)
return(rdata_set)
})
observeEvent(input$view_options, {
req(nrow(rdata_set) > 0)
req(input$data_set)
x = colnames(rdata_set)
updateCheckboxGroupInput(
session,
'select_vars',
label = 'Select variables on which to explore metabolites',
choices = x,
selected = c()
)
})
update_select_helper = reactive({
input$update_select
vars = isolate(input$select_vars)
sel_data = rdata_set %>% select(!!vars)
var_type =
(sel_data %>%
dplyr::summarise_all(class) %>%
tidyr::gather(variable, class))
rdata_set_select <<- var_type
})
proc_selected = reactive({
input$update_select
req('tbl_df' %in% class(rdata_set_select))
rdata_select_prepared <<- as_tibble(t(rdata_set_select))
var_names_ = as.character(rdata_select_prepared[1,])
types_ = as.character(rdata_select_prepared[2,])
type_options_ = lapply(seq_along(var_names_), function(i) {
if (types_[i] == 'numeric') {
xz <<- c(
xz,
paste0('Search ', var_names_[i], sep = ''),
paste0('Filter greater than ', var_names_[i], sep = ''),
paste0('Filter less than ', var_names_[i], sep = ''),
paste0('Sort low to high ', var_names_[i], sep = ''),
paste0('Sort high to low ', var_names_[i], sep = '')
)
} else if (types_[i] == 'character') {
xz <<- c(xz, paste0('Search ', var_names_[i], sep = ''))
}
})
updateCheckboxGroupInput(
session,
'select_sorting_options',
label = 'Select search and sort options for each variable',
choices = xz,
selected = c()
)
})
sidebar_to_explore2 =
function(reactor) {
renderUI({
create_UI_component = function(x) {
colname = word(x, -1)
ID = gsub(' ', '_', x)
search_settings <<- c(search_settings, ID)
if(grepl('Search', x)){
return(renderUI({
tagList(
selectInput(ID, paste('Search', colname, sep = ' '),
choices = unique(rdata_set %>% select(!!colname))),
br(),
hr()
)
}))
} else if(grepl('Filter greater than', x)) {
return(renderUI({
tagList(
sliderInput(ID, paste('Fliter greater than', colname, sep = ' '),
min = min(rdata_set %>% select(!!colname)),
max = max(rdata_set %>% select(!!colname)),
value = min(rdata_set %>% select(!!colname))),
br(),
hr()
)
})
)
} else if(grepl('Filter less than', x)) {
return(renderUI({
tagList(
sliderInput(ID, paste('Filter less than', colname, sep = ' '),
min = min(rdata_set %>% select(!!colname)),
max = max(rdata_set %>% select(!!colname)),
value = max(rdata_set %>% select(!!colname))),
br(),
hr()
)
})
)
} else if(grepl('Sort low to high', x)) {
return(renderUI({
tagList(
actionButton(ID, paste('Sort low to high', colname, sep = ' ')),
br(),
hr()
)
})
)
} else if(grepl('Sort high to low', x)) {
return(renderUI({
tagList(
actionButton(ID, paste('Sort high to low', colname, sep = ' ')),
br(),
hr()
)
})
)
} else {
return()
}
}
vars_ = isolate(input$select_sorting_options)
xz_select = sapply(xz, function(x) {
if(x %in% vars_) {
return(TRUE)
} else {
FALSE
}
})
xz_select = xz[xz_select]
lapply(xz_select, create_UI_component)
})
}
observeEvent(input$update_select, {
search_settings <<- list()
xz <<- c()
update_select_helper()
proc_selected()
})
observeEvent(input$update_select_w_options, {
req(input$update_select)
rdata_set_cumulate <<- rdata_set
output$sidebar_to_explore2 = sidebar_to_explore2(reactive(input$update_select_w_options))
})
get_data_on_click =
eventReactive(input$data_preview, {
return(rdata_set)
})
output$contents =
renderDataTable({
return(get_data_on_click())
}, options = list(pageLength = 20))
filtering = function() {
rdata_set_cumulate = rdata_set
search_settings_ = search_settings
for (ipv in 1:length(search_settings_)) {
current_ = search_settings_[[ipv]]
print('current:')
print(current_)
column_ = sym(word(gsub('_', ' ', current_),-1))
input_ = input[[search_settings_[[ipv]]]]
print('input:')
print(input_)
if (is.null(input_) | (input_ == FALSE)) {
next
}
if (grepl('Search', current_)) {
rdata_set_cumulate = rdata_set_cumulate %>% filter(!!column_ == !!input_)
} else if (grepl('Filter_greater_than', current_)) {
rdata_set_cumulate = rdata_set_cumulate %>% filter(!!column_ >= !!input_)
} else if (grepl('Filter_less_than', current_)) {
rdata_set_cumulate = rdata_set_cumulate %>% filter(!!column_ <= !!input_)
} else if (grepl('Sort_low_to_high', current_)) {
rdata_set_cumulate = rdata_set_cumulate %>% arrange(!!column_)
} else if (grepl('Sort_high_to_low', current_)) {
rdata_set_cumulate = rdata_set_cumulate %>% arrange(desc(!!column_))
}
}
file_list <<- rdata_set_cumulate$Filename
}
core = reactive({
req(input$run)
input$run
isolate(filtering())
rdata_set_cumulate <<- rdata_set
lapply(seq_along(file_list), function(i) {
output[[paste0("images", i)]] <- renderImage({
print(paste(isolate(mdat_path()), file_list[i], sep = '/'))
list(
src = paste(isolate(mdat_path()), file_list[i], sep = '/'),
filetype = "image/jpeg",
height = 700,
width = 1000
)
}, deleteFile = FALSE)
})
})
output$imageUI <- renderUI({
core()
return(
flowLayout(
lapply(seq_along(file_list), function(i) {
imageOutput(paste0("images", i), width = "100%", height = "100%")
})
)
)
})
}
shinyApp(ui = ui, server = server)