-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.R
72 lines (70 loc) · 2.34 KB
/
helper_functions.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
#' @title printVecAsis
#' @description For internal use. Print vector as character string
#' @param x vector
#' @param asChar exclude c notation at the beginning of string
#' @keywords internal
#' @export
printVecAsis <- function(x, asChar = FALSE) {
if (is.character(x)) {
if (length(x) == 1) {
return(paste0("\'", x, "\'"))
} else {
if (asChar == FALSE) {
return(paste0("c(", paste(sapply(x, function(a) paste0("\'", a, "\'")),
collapse = ", "), ")"))
} else {
return(paste0("(", paste(sapply(x, function(a) paste0("\'", a, "\'")),
collapse = ", "), ")"))
}
}
} else {
if (length(x) == 1) {
return(x)
} else {
if (asChar == FALSE) {
return(paste0("c(", paste(x, collapse = ", "), ")"))
} else {
return(paste0("(", paste(x, collapse = ", "), ")"))
}
}
}
}
#' @title writeLog
#' @description For internal use. Add text to a logger
#' @param logger The logger to write the text to. Can be NULL or a function
#' @param ... Messages to write to the logger
#' @param type One of "default", "error", "warning"
#' @keywords internal
#' @export
writeLog <- function(logger, ..., type = 'default') {
if (is.null(logger)) {
if (type == 'error') {
stop(paste0(..., collapse = ""), call. = FALSE)
} else if (type == 'warning') {
warning(paste0(..., collapse = ""), call. = FALSE)
} else {
message(paste0(..., collapse = ""))
}
} else if (is.function(logger)) {
if (type == "default") {
pre <- "> "
} else if (type == 'info') {
shinyalert::shinyalert(...,
type = "info")
pre <- '> <font color="blue"><b>INFO</b></font> : '
} else if (type == 'error') {
shinyalert::shinyalert("Please, check Log window for more information ",
type = "error")
pre <- '> <font color="red"><b>! ERROR</b></font> : '
} else if (type == 'warning') {
shinyalert::shinyalert("Please, check Log window for more information ",
type = "warning")
pre <- '> <font color="orange"><b>! WARNING</b></font> : '
}
newEntries <- paste0('<br>', pre, ..., collapse = "")
logger(paste0(logger(), newEntries))
} else {
warning("Invalid logger type")
}
invisible()
}