-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.R
448 lines (379 loc) · 10.6 KB
/
helpers.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#'
#' Some general helpers
#'
library(shiny)
library(reshape2)
library(SummarizedExperiment)
library(Cairo)
library(parallel)
library(tools)
source("classImporterParameter.R")
#' Returns TRUE if all data in x are integers
#'
#' @param x
#'
#' @return
#' @export
#'
#' @examples
is.integer <- function(x) {
return(all(x == floor(x)))
}
#' Checks if object is a SummarizedExperiment
#'
#' @param object
#'
#' @return
#' @export
#'
#' @examples
is.SummarizedExperiment <- function(object) {
return(is(object, "SummarizedExperiment"))
}
#' Checks if value is a valid choice
#'
#' @param value
#' @param choices
#'
#' @return
#' @export
#'
#' @examples
enum.contains <- function(value, choices) {
return(value %in% choices)
}
#' Generates a random string
#' Src: http://stackoverflow.com/questions/42734547/generating-random-strings
#'
#' @param n
#'
#' @return
#' @export
#'
#' @examples
randomString <- function(n = 1, k = 5) {
a <- do.call(paste0, replicate(k, sample(LETTERS, n, TRUE), FALSE))
paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}
#' Returns logical vector indicating for each input element if they are a valid color
#'
#' @param x Vector of character strings
#'
#' @return Logical vector determining if the corresponding string is a valid color
#' @export
#'
#' @examples
isColor <- function(x) {
if(!is.character(x)) {
stop("Invalid arguments!")
}
if(length(x) == 0) {
return(T)
}
sapply(x, function(X) {
tryCatch(is.matrix(col2rgb(X)),
error = function(e) FALSE)
})
}
#' Reads a data frame and extracts labels for the columns from them.
#' Columns that have the form "colum=Custom label" are seen as labeled columns.
#' This function will reshape the data frame to only contain the actual column name (here "column")
#' and return the labels
#' If no custom label is provided, the label is the column name
#'
#' @param data
#'
#' @return List with data frame with correct column names ($data) and named vector of column labels ($labels).
#' @export
#'
#' @examples
data.frame.labels = function(data) {
col <- colsplit(colnames(data), "=", c("id", "label"))
col.labels <- apply(col, 1, function(x) { if(is.na(x[["label"]])) x[["id"]] else x[["label"]] })
names(col.labels) <- col$id
output <- list()
output$data <- data
colnames(output$data) <- col$id
output$labels <- col.labels
return(output)
}
#' Custom implementation of withProgress
#'
#' callback function: updateProgress(detail = NULL, value = NULL)
#' Updates the progress with description of current task (detail) and the progress (value; numeric in 0 ... 1)
#'
#' @param expr Function that takes 1 parameter (callback function)
#' @param message Message of the progress bar
#'
#' @return
#' @export
#'
#' @examples
withProgressCustom <- function(expr, message) {
progress <- shiny::Progress$new()
on.exit({
progress$close()
})
progress$set(message = message, value = 0)
# Status callback function
updateProgress <- function(detail = NULL, value = NULL) {
progress$set(value = value, detail = detail)
}
return(expr(updateProgress))
}
#' Runs expr in parallel environment
#'
#' @param expr Expression to run
#' @param exprsuccess Expression to run if the task was successful (not canceled)
#' @param exprfailed Expression to run if the task was unsuccessful (canceled)
#' @param exprfinally Expression to run after parallel
#'
#' @return
#' @export
#'
#' @examples
withParallel <- function(session, input, expr, exprsuccess = function() {}, exprfailed = function() {}, exprfinally = function(){}, message = "The calculation is currently running. Please wait.") {
vars <- reactiveValues(status = "calculating")
showModal(modalDialog(
iconText(icon("circle-o-notch", class = "fa-spin"), message),
footer = tagList(
actionButton(session$ns("parallel.cancel"), "Cancel")
)
))
# Start the task
process <- mcparallel(expr)
# User can click cancel
observeEvent(input$parallel.cancel, {
vars$status <- "canceled"
print(paste("[Parallel] canceled by user. Ending PID", process$pid))
pskill(process$pid)
removeModal()
exprfailed()
exprfinally()
})
# Always check every 1s
observe({
process.result <- mccollect(process, wait = F)
if(isolate(vars$status) == "calculating") {
if(!is.null(process.result)) {
vars$status <- "finished"
print("[Parallel] finished. Process returned:")
print(process.result)
removeModal()
exprsuccess(process.result[[1]])
exprfinally()
}
else {
invalidateLater(1000)
}
}
})
}
#' Creates a notification that indicates progress without a progress bar
#' Server code must close this notification manually!
#'
#' @param id Id of the notification if NULL, an ID will be generated
#' @param message
#'
#' @return ID of the notification
#' @export
#'
#' @examples
progressNotification <- function(message, id = NULL) {
if(is.null(id)) {
id <- stringi::stri_rand_strings(1, 16)
}
showNotification(
ui = tags$span(icon("circle-o-notch", class = "fa-spin"), message),
id = id,
closeButton = F,
type = "default",
duration = NULL
)
return(id)
}
#' Updates an existing progress notification
#'
#' @param id
#' @param message
#'
#' @return
#' @export
#'
#' @examples
updateProgressNotification <- function(id, message) {
showNotification(
ui = tags$span(icon("circle-o-notch", class = "fa-spin"), message),
id = id,
closeButton = F,
type = "default",
duration = NULL
)
}
#' Runs an expression with progress notification
#'
#' @param message
#' @param expr
#'
#' @return
#' @export
#'
#' @examples
withProgressNotification <- function(message, expr) {
id <- randomString(n = 1, k = 16)
on.exit({
removeNotification(id = id)
})
progressNotification(id = id, message = message)
return(expr())
}
#' Returns input if it isn't empty or NULL
#'
#' @param input
#' @param default
#'
#' @return
#' @export
#'
#' @examples
getOrDefault.character <- function(input, default) {
return(if(is.null(input) || length(input) == 0) input else default)
}
#' Saves a plot using R default plotting system
#'
#' @param width
#' @param height
#' @param dpi
#' @param format
#' @param expr
#'
#' @return
#' @export
#'
#' @examples
saveRPlot <- function(width, height, dpi, scale, filename, format, expr) {
if(!is.numeric(width) || !is.numeric(height) || !is.numeric(dpi) || !is.numeric(scale)
|| !is.character(filename) || !is.character(format) || missing(expr)) {
stop("Invalid arguments!")
}
if(format == "svg") {
CairoSVG(file = filename,
width = width / dpi,
height = height / dpi,
pointsize = 12 * scale)
}
else if(format == "pdf") {
CairoPDF(file = filename,
width = width / dpi,
height = height / dpi,
pointsize = 12 * scale)
}
else if(format == "png") {
CairoPNG(filename = filename,
width = width,
height = height,
pointsize = 12 * scale,
units = "px",
res = dpi)
}
else if(format == "tiff") {
CairoTIFF(filename = filename,
width = width,
height = height,
pointsize = 12 * scale,
units = "px",
res = dpi)
}
expr()
dev.off()
}
#' A importer parameter that handles CSV separators
ImporterParameter.csv <- ImporterParameter(name = "separator",
label = "CSV Separator",
type = "select",
select.values = c(
"Comma" = ",",
"Semicolon" = ";",
"Tab" = "\t",
"Whitespace" = " "
))
#' Importer parameters that handles comments + line selection + column selection
ImporterParameter.csv.comment <- ImporterParameter(name = "comment.char",
label = "Comment character",
type = "select",
select.values = c(
"None" = "none",
"#" = "#"
))
ImporterParameter.csv.selectrows <- ImporterParameter(name = "selected.rows",
label = "Restrict rows (e.g. 1,3-5,9)",
type = "lineedit",
lineedit.default = "")
ImporterParameter.csv.selectcolumns <- ImporterParameter(name = "selected.columns",
label = "Restrict columns (e.g. 1,3-5,9)",
type = "lineedit",
lineedit.default = "")
#' Parses the "restrict rows"/"restrict columns" input
#'
#'
#' @param input String of format 1,2,10,20-30,41
#'
#' @return NULL if all numbers are selected, NA if there is an error, otherwise a vector of selected numbers
#' @export
#'
#' @examples
parse.selectIntegers <- function(input) {
input <- gsub(" ", "", input, fixed = T)
if(input == "") {
return(NULL)
}
if(!grepl(",", input, fixed = T)) {
if(grepl("-", input, fixed = T)) {
cell2 <- unlist(strsplit(input, "-", fixed = T))
if(length(cell2) != 2) {
return(NA)
}
w1 <- suppressWarnings(as.integer(cell2[1]))
w2 <- suppressWarnings(as.integer(cell2[2]))
if(is.na(w1) || is.na(w2)) {
return(NA)
}
if(w1 > w2) {
return(NA)
}
data <- c(data, w1:w2)
}
else {
return(suppressWarnings(as.integer(input)))
}
}
cell <- unlist(strsplit(input, ",", fixed = T))
data <- c()
for(i in seq_along(cell)) {
num <- suppressWarnings(as.integer(cell[i]))
if(is.na(num)) {
if(grepl("-", cell[i], fixed = T)) {
cell2 <- unlist(strsplit(cell[i], "-", fixed = T))
if(length(cell2) != 2) {
return(NA)
}
w1 <- suppressWarnings(as.integer(cell2[1]))
w2 <- suppressWarnings(as.integer(cell2[2]))
if(is.na(w1) || is.na(w2)) {
return(NA)
}
if(w1 > w2) {
return(NA)
}
data <- c(data, w1:w2)
}
else {
return(NA)
}
}
else {
data <- c(data, num)
}
}
return(data)
}