forked from rstudio/shinyapps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
94 lines (81 loc) · 2.1 KB
/
utils.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
isStringParam <- function(param) {
is.character(param) && (length(param) == 1)
}
stringParamErrorMessage <- function(param) {
paste(param, "must be a single element character vector")
}
regexExtract <- function(re, input) {
match <- regexec(re, input)
matchLoc <- match[1][[1]]
if (length(matchLoc) > 1) {
matchLen <-attributes(matchLoc)$match.length
return (substr(input, matchLoc[2], matchLoc[2] + matchLen[2]-1))
}
else {
return (NULL)
}
}
displayStatus <- function(quiet) {
quiet <- quiet || httpDiagnosticsEnabled()
function (status) {
if (!quiet)
cat(status)
}
}
withStatus <- function(quiet) {
quiet <- quiet || httpDiagnosticsEnabled()
function(status, code) {
if (!quiet)
cat(status, "...", sep="")
force(code)
if (!quiet)
cat("DONE\n")
}
}
httpDiagnosticsEnabled <- function() {
return (getOption("shinyapps.http.trace", FALSE) ||
getOption("shinyapps.http.verbose", FALSE))
}
readPassword <- function(prompt) {
# user super secret function if using RStudio
if (exists(".rs.askForPassword")) {
password <- .rs.askForPassword(prompt)
} else {
os <- Sys.info()[['sysname']]
echoOff <- function() {
if (identical(os, "Darwin") || identical(os, "Linux")) {
#system("stty cbreak -echo <&2")
} else {
# TODO: disable echo on Windows
}
}
echoOn <- function() {
if (identical(os, "Darwin") || identical(os, "Linux")) {
#system("stty echo")
} else {
# TODO: enable echo on Windows
}
}
echoOff()
password <- readline(prompt)
echoOn()
}
return (password)
}
# wrapper around read.dcf to workaround LC_CTYPE bug
readDcf <- function(...) {
loc <- Sys.getlocale('LC_CTYPE')
on.exit(Sys.setlocale('LC_CTYPE', loc))
read.dcf(...)
}
#' @export
hr <- function(message = "", n = 80) {
if (nzchar(message)) {
r <- as.integer((n - nchar(message) - 2) / 2)
hr <- paste(rep_len("#", r), collapse = '')
cat(hr, message, hr, sep=" ", '\n')
} else {
hr <- paste(rep_len("#", n), collapse = '')
cat(hr, sep="", '\n')
}
}