| 1 |
# @staticimports pkg:staticimports |
| 2 |
# is_installed get_package_version |
| 3 |
|
| 4 |
write_utf8 <- function(text, ...) { |
| 5 |
writeBin(charToRaw(enc2utf8(text)), ...) |
| 6 |
} |
| 7 |
|
| 8 |
raw_to_utf8 <- function(data) { |
| 9 |
res <- rawToChar(data) |
| 10 |
Encoding(res) <- "UTF-8" |
| 11 |
res |
| 12 |
} |
| 13 |
|
| 14 |
read_raw <- function(file) { |
| 15 |
# If the file doesn't exist, readBin() will throw a warning and an error. We |
| 16 |
# really only want it to throw the error (when used in FileCache), so suppress |
| 17 |
# the warning. Unfortunately, the error has litle useful information; it only |
| 18 |
# says 'Error in file(con, "rb") : cannot open the connection'. The warning |
| 19 |
# contains the information about wht the actual problem is, as in 'cannot open |
| 20 |
# file 'asdf': No such file or directory'. For the FileCache use case, it's OK |
| 21 |
# to throw away that information, but for general use, it would be better to |
| 22 |
# use brio::read_file_raw(), because it only throws an error (no warning), and |
| 23 |
# the error is informative. |
| 24 |
suppressWarnings( |
| 25 |
readBin(file, "raw", n = file.size(file)) |
| 26 |
) |
| 27 |
} |
| 28 |
|
| 29 |
read_utf8 <- function(file) { |
| 30 |
res <- read_raw(file) |
| 31 |
raw_to_utf8(res) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
# `file` must be a character vector with files and directories. This will return |
| 36 |
# a data frame with the mtimes of all files that are passed in, as well as the |
| 37 |
# mtimes of files in the directories that are passed in. The data frame will not |
| 38 |
# contain the mtimes of the directories themselves. Also, any files that were |
| 39 |
# passed in but don't exist will not be present in the returned data frame. |
| 40 |
get_file_mtimes <- function(files) { |
| 41 |
if (length(files) == 0) return(NULL) |
| 42 |
|
| 43 |
is_dir <- dir.exists(files) |
| 44 |
all_files <- c( |
| 45 |
files[!is_dir], |
| 46 |
dir(files[is_dir], full.names = TRUE, all.files = TRUE, recursive = TRUE, no.. = TRUE) |
| 47 |
) |
| 48 |
rlang::set_names(file.mtime(all_files), all_files) |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
# Returns TRUE if this is called while a Shiny app is running; FALSE otherwise. |
| 53 |
is_shiny_app <- function() { |
| 54 |
isNamespaceLoaded("shiny") && shiny::isRunning() |
| 55 |
} |
| 56 |
|
| 57 |
# Is this app hosted? Returns TRUE for both Shiny Server and RStudio Connect. |
| 58 |
is_hosted_app <- function() { |
| 59 |
nzchar(Sys.getenv("SHINY_SERVER_VERSION")) && is_shiny_app() |
| 60 |
} |
| 61 |
|
| 62 |
# Is this app running on Shiny Server (and not RStudio Connect)? |
| 63 |
is_shiny_server_app <- function() { |
| 64 |
is_hosted_app() && !is_connect_app() |
| 65 |
} |
| 66 |
|
| 67 |
# Is this app running on RStudio Connect (and not Shiny Server)? |
| 68 |
is_connect_app <- function() { |
| 69 |
if (!is_hosted_app()) { |
| 70 |
return(FALSE) |
| 71 |
} |
| 72 |
|
| 73 |
info <- shiny::serverInfo() |
| 74 |
if (!is.null(info) && is.list(info) && identical(info$edition, "Connect")) { |
| 75 |
return(TRUE) |
| 76 |
} |
| 77 |
|
| 78 |
FALSE |
| 79 |
} |
| 80 |
|
| 81 |
# Wrapper for file.copy that throws if any files fail to copy. Use name |
| 82 |
# file.copy2 to make clear that it's a wrapper for a base function, and not for |
| 83 |
# fs::file_copy. |
| 84 |
file.copy2 <- function(from, to, ...) { |
| 85 |
res <- file.copy(from, to, ...) |
| 86 |
if (!all(res)) { |
| 87 |
stop("Error copying files: ", paste0(from[!res], collapse = ", ")) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
dir.create2 <- function(path, ...) { |
| 92 |
res <- dir.create(path, ...) |
| 93 |
if (!res) { |
| 94 |
stop("Error creating directory: ", path) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
# adds a class to the object (if possible) (if not already done) |
| 99 |
add_class <- function(x, class_val) { |
| 100 |
if (is.null(x)) return(x) |
| 101 |
if (inherits(x, class_val)) return(x) |
| 102 |
|
| 103 |
class(x) <- c(class_val, class(x)) |
| 104 |
x |
| 105 |
} |
| 106 |
|
| 107 |
collapse0 <- function(..., collapse = "\n") { |
| 108 |
paste0(..., collapse = collapse) |
| 109 |
} |
| 110 |
|
| 111 |
# checks for any valid, non-"" keys in the list x |
| 112 |
#' @importFrom rlang have_name |
| 113 |
has_any_name <- function(x) { |
| 114 |
any(have_name(x)) |
| 115 |
} |
| 116 |
|
| 117 |
is_string <- function(x) { |
| 118 |
is.character(x) && length(x) == 1 |
| 119 |
} |
| 120 |
|
| 121 |
trim_ws <- function(x) { |
| 122 |
sub("^\\s*", "", sub("\\s*$", "", x)) |
| 123 |
} |
| 124 |
|
| 125 |
has_any_name_recursive <- function(x) { |
| 126 |
if (has_any_name(x)) { |
| 127 |
# if this level has a name, return true |
| 128 |
return(TRUE) |
| 129 |
} |
| 130 |
if (!is.list(x)) { |
| 131 |
return(FALSE) |
| 132 |
} |
| 133 |
# Recursively inspect list objects |
| 134 |
# Use for loop to pre-empty calculations |
| 135 |
for (item in x) { |
| 136 |
if (has_any_name_recursive(item)) { |
| 137 |
return(TRUE) |
| 138 |
} |
| 139 |
} |
| 140 |
return(FALSE) |
| 141 |
} |