-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpeakReviewApp.R
351 lines (249 loc) · 8.78 KB
/
peakReviewApp.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
# Description ------------------------------------
# Opens viewer for plot files of peak windows from a csv table given a directory
# of MS1 tables
# User Input ------------------------------------------------------------------
# Path to folder with individual peak table directories
# for Windows operating system use \\ or / instead of \ to separate directories
# example:
# peakTableParentDir <- 'M:\\folder1\\foldwerWithFile'
imageDir <- "M:\\folder1\\foldwerWithFile"
# path to folder with tracking table
trackFolderPath <- "M:\\folder2\\foldwerWithFile"
# tracking table file name
trackingTableCSV <- "filename.csv"
# End user input ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Library (packages)--------------------------------------------------------
library(shiny) # used for generating html apps
library(data.table)
# load tracking table ----------------------------------------------------
pathTrackingTable <- paste0(trackFolderPath,"/",trackingTableCSV)
dt.tracking <- fread(file = pathTrackingTable)
# get number of image rows in table
numTotalImages <- nrow(dt.tracking)
# initialize image number --------------------------------------------------
# sort by rowNum
setorder(dt.tracking, rowNum)
# subset to unviewed
dt.unviewed <- dt.tracking[viewed == 0]
# take first rowNum or set to last if all have been viewed
if (nrow(dt.unviewed) > 0) {
startRow <- dt.unviewed$rowNum[1]
} else {
startRow <- numTotalImages
}
# remove "unviewed"
rm(dt.unviewed)
numImageFile <- startRow
numImageFilePrevSave <- startRow
dt.tracking$viewed[startRow] <- 1
# define the User interface -------------------------------------------------
ui <- fluidPage(
verticalLayout(
# Use imageOutput to place the image on the page
imageOutput(outputId = "myImage"),
# breaklines so buttons aren't on top of plot
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
fluidRow(textOutput("textBad"),
textOutput("textWindFlag"),
textOutput("textBorderline")
),
fluidRow(
# HTML for hotkeys
# find hotkeys at: http://keycode.info/
tags$script(HTML("$(function(){
$(document).keyup(function(e) {
if (e.which == 32) {
$('#buttonBad').click()
}
if (e.which == 71) {
$('#buttonGood').click()
}
if (e.which == 37) {
$('#buttonPrev').click()
}
if (e.which == 39) {
$('#buttonNext').click()
}
if (e.which == 83) {
$('#buttonSave').click()
}
if (e.which == 87) {
$('#buttonWindFlag').click()
}
if (e.which == 66) {
$('#buttonBorderline').click()
}
});
})")),
column(width = 1,
actionButton(inputId = "buttonSave", label = "Save (s)"),
textOutput("textSave")
),
column(width = 2, offset = 1,
actionButton(inputId = "buttonBad", label = "Bad (Space)"),
actionButton(inputId = "buttonGood", label = "Good (g)")
),
column(width = 3, offset = 1,
actionButton(inputId = "buttonPrev", label = "Previous (<-)"),
actionButton(inputId = "buttonNext", label = "Next (->)"),
textOutput("textFileNum")
),
column(width = 3, offset = 1,
actionButton(inputId = "buttonWindFlag",
label = "(un)Flag Window Edges (w)"),
actionButton(inputId = "buttonBorderline",
label = "(un)Flag Borderline (b)")
),
# formatting for Bad text and Window flag
tags$head(tags$style("#textBad{color: red;
font-size: 40px;
font-style: bold;
}"
)),
tags$head(tags$style("#textWindFlag{color: red;
font-size: 20px;
font-style: bold;
}"
)),
tags$head(tags$style("#textBorderline{color: red;
font-size: 20px;
font-style: bold;
}"
))# end formatting
) # end well Panel
) # end vertical layout
) # end fluid page
# Define the server code ----------------------------------------------------
server <- function(input, output, session) {
# use <<- to change global objects
# create reactive state variables -----------------------------------------
state <- reactiveValues()
state$numImageFile <- startRow
observe({
# track the total button clicks
state$sumButtonActivityCount <- input$buttonNext +
input$buttonPrev + input$buttonBad
# update image label
state$peakGood <- dt.tracking$good1bad0[state$numImageFile]
# update window bounds
state$windowBad <- dt.tracking$windowBad[state$numImageFile]
# update borderline variable
state$borderline <- dt.tracking$borderline[state$numImageFile]
})
# track image information -------------------------------------------------------
# update values when previous and next buttons are pressed
observeEvent(input$buttonNext, {
state$numImageFile <- min(numTotalImages, state$numImageFile + 1)
# update viewed column
dt.tracking$viewed[state$numImageFile] <<- 1
})
observeEvent(input$buttonPrev, {
# advance image number
state$numImageFile <- max(0, state$numImageFile - 1)
# update viewed column
dt.tracking$viewed[state$numImageFile] <<- 1
})
# text for tracking image number
output$textFileNum <- renderText({
# text output
paste0("image number: ", state$numImageFile, " of ", numTotalImages)
})
# Display image -----------------------------------------------------------
# A dynamically-sized plot
output$myImage <- renderImage({
# get path for current image file
imagePath <- dt.tracking$path[state$numImageFile]
# Read myImage's width and height. These are reactive values, so this
# expression will re-run whenever they change.
width <- session$clientData$output_myImage_width
# height <- session$clientData$output_myImage_height
# Return a list containing the filename
list(src = imagePath,
contentType = 'image/jpg',
width = width,
alt = "No More Images")
}, deleteFile = FALSE) # end render image
# Indicate Image Bad -------------------------------------------------------
observeEvent(input$buttonBad, {
dt.tracking$good1bad0[state$numImageFile] <<- 0
state$peakGood <- dt.tracking$good1bad0[state$numImageFile]
})
observeEvent(input$buttonGood, {
dt.tracking$good1bad0[state$numImageFile] <<- 1
state$peakGood <- dt.tracking$good1bad0[state$numImageFile]
})
output$textBad <- renderText({
if (state$peakGood == 0) {
"BAD"
} else {""}
})
# flag window edges ---------------------------------------------------------------
observeEvent(input$buttonWindFlag, {
if (state$windowBad == 0) {
dt.tracking$windowBad[state$numImageFile] <<- 1
} else {
dt.tracking$windowBad[state$numImageFile] <<- 0
}
state$windowBad <- dt.tracking$windowBad[state$numImageFile]
})
output$textWindFlag <- renderText({
if (state$windowBad == 1) {
"Window Edges Flagged"
} else {""}
})
# flag as borderline ---------------------------------------------------------------
observeEvent(input$buttonBorderline, {
if (state$borderline == 0) {
dt.tracking$borderline[state$numImageFile] <<- 1
} else {
dt.tracking$borderline[state$numImageFile] <<- 0
}
state$borderline <- dt.tracking$borderline[state$numImageFile]
})
output$textBorderline <- renderText({
if (state$borderline == 1) {
"Peak Flagged as Borderline Acceptable"
} else {""}
})
# Saving table --------------------------------------------------------------------
# initialize save tracker
state$saveActivityVal <- 0
observeEvent(input$buttonSave, {
# write the tracking table
fwrite(dt.tracking,pathTrackingTable)
# update state$saveValPrev
state$saveActivityVal <- state$sumButtonActivityCount
# print to console
print(paste0("save",Sys.time(), " ", state$saveActivityVal))
})
# text for saving
output$textSave <- renderText({
if (state$saveActivityVal == state$sumButtonActivityCount) {
"Progress Saved"
} else {""} # blank if not saved
}) # end save text element
} # end server function
# Return a Shiny app object ---------------------------------------------------
shinyApp(ui = ui, server = server)