-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathutils.R
75 lines (59 loc) · 1.67 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
as_df <- function(x) {
class(x) <- "data.frame"
attr(x, "row.names") <- c(NA_integer_, -length(x[[1]]))
x
}
check_quiet <- function(x, arg = caller_arg(x), call = caller_env(call)) {
check_bool(x, allow_na = TRUE, arg = arg, call = call)
if (is.na(x)) {
!(is_interactive() || is_snapshot())
} else {
x
}
}
bq_check_namespace <- function(pkg, bq_type) {
check_installed(pkg, sprintf("to parse BigQuery '%s' fields.", bq_type))
}
cat_line <- function(...) {
cat(paste0(..., "\n", collapse = ""))
}
big_mark <- function(x, ...) {
mark <- if (identical(getOption("OutDec"), ",")) "." else ","
format(x, big.mark = mark, scientific = FALSE, ...)
}
map_chr <- function(x, f, ...) {
vapply(x, f, ..., FUN.VALUE = character(1))
}
indent <- function(x, n = 2) {
space <- paste(rep(" ", n), collapse = "")
paste0(space, gsub("\n", paste0("\n", space), x, fixed = TRUE))
}
as_json <- function(x) UseMethod("as_json")
#' @export
as_json.NULL <- function(x) NULL
# nocov start
show_json <- function(x) {
jsonlite::toJSON(x, pretty = TRUE, auto_unbox = TRUE)
}
#' @export
print.bq_bytes <- function(x, ...) {
cat_line(prettyunits::pretty_bytes(unclass(x)))
}
# nocov end
defer <- function(expr, env = caller_env(), after = FALSE) {
thunk <- as.call(list(function() expr))
do.call(on.exit, list(thunk, TRUE, after), envir = env)
}
in_pkgdown <- function() {
identical(Sys.getenv("IN_PKGDOWN"), "true")
}
as_query <- function(x, error_arg = caller_arg(x), error_call = caller_env()) {
if (is(x, "SQL")) {
x <- x@.Data
}
check_string(x, arg = error_arg, call = error_call)
x
}
has_bigrquerystorage <- function() {
is_installed("bigrquerystorage")
}