-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathutils.R
167 lines (138 loc) · 3.98 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
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
# Silence R CMD check note
#' @importFrom tibble tibble
NULL
isFALSE <- function(x) identical(x, FALSE)
is.connection <- function(x) inherits(x, "connection")
`%||%` <- function(a, b) if (is.null(a)) b else a
is_syntactic <- function(x) make.names(x) == x
#' Determine whether progress bars should be shown
#'
#' Progress bars are shown _unless_ one of the following is `TRUE`
#' - The bar is explicitly disabled by setting `options(readr.show_progress = FALSE)`
#' - The code is run in a non-interactive session (`interactive()` is `FALSE`).
#' - The code is run in an RStudio notebook chunk.
#' - The code is run by knitr / rmarkdown.
#' @export
show_progress <- function() {
isTRUE(getOption("readr.show_progress")) && # user disables progress bar
interactive() && # an interactive session
!isTRUE(getOption("rstudio.notebook.executing")) && # Not running in an RStudio notebook chunk
!isTRUE(getOption("knitr.in.progress")) # Not actively knitting a document
}
#' Determine whether column types should be shown
#'
#' Column types are shown unless
#' - They are disabled by setting `options(readr.show_types = FALSE)`
#' - The column types are supplied with the `col_types` argument.
#' @export
should_show_types <- function() {
if (identical(getOption("readr.show_types", TRUE), FALSE)) {
FALSE
} else {
NULL
}
}
deparse2 <- function(expr, ..., sep = "\n") {
paste(deparse(expr, ...), collapse = sep)
}
is_integerish <- function(x) {
floor(x) == x
}
#' Determine how many threads readr should use when processing
#'
#' The number of threads returned can be set by
#' - The global option `readr.num_threads`
#' - The environment variable `VROOM_THREADS`
#' - The value of [parallel::detectCores()]
#' @export
readr_threads <- function() {
res <- getOption("readr.num_threads")
if (is.null(res)) {
res <- as.integer(Sys.getenv("VROOM_THREADS", parallel::detectCores()))
options("readr.num_threads" = res)
}
if (is.na(res) || res <= 0) {
res <- 1
}
res
}
#' @importFrom tibble as_tibble
#' @export
as_tibble.spec_tbl_df <- function(x, ...) {
attr(x, "spec") <- NULL
attr(x, "problems") <- NULL
class(x) <- setdiff(class(x), "spec_tbl_df")
NextMethod("as_tibble")
}
#' @export
as.data.frame.spec_tbl_df <- function(x, ...) {
attr(x, "spec") <- NULL
attr(x, "problems") <- NULL
class(x) <- setdiff(class(x), "spec_tbl_df")
NextMethod("as.data.frame")
}
#' @export
`[.spec_tbl_df` <- function(x, ...) {
attr(x, "spec") <- NULL
attr(x, "problems") <- NULL
class(x) <- setdiff(class(x), "spec_tbl_df")
NextMethod(`[`)
}
#' @importFrom methods setOldClass
setOldClass(c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
# @export
compare.tbl_df <- function(x, y, ...) {
attr(x, "spec") <- NULL
attr(x, "problems") <- NULL
attr(y, "spec") <- NULL
attr(y, "problems") <- NULL
NextMethod("compare")
}
# @export
compare.col_spec <- function(x, y, ...) {
x[["skip"]] <- NULL
y[["skip"]] <- NULL
NextMethod("compare")
}
# @export
compare_proxy.spec_tbl_df <- function(x, path) {
attr(x, "spec") <- NULL
attr(x, "problems") <- NULL
class(x) <- setdiff(class(x), "spec_tbl_df")
x
if ("path" %in% names(formals(waldo::compare_proxy))) {
list(object = x, path = path)
} else {
x
}
}
is_named <- function(x) {
nms <- names(x)
if (is.null(nms)) {
return(FALSE)
}
all(nms != "" & !is.na(nms))
}
utctime <- function(year, month, day, hour, min, sec, psec) {
utctime_(
as.integer(year), as.integer(month), as.integer(day),
as.integer(hour), as.integer(min), as.integer(sec), as.numeric(psec)
)
}
cli_block <- function(expr, class = NULL, type = rlang::inform) {
msg <- ""
withCallingHandlers(
expr,
message = function(x) {
msg <<- paste0(msg, x$message)
invokeRestart("muffleMessage")
}
)
type(msg, class = class)
}
readr_enquo <- function(x) {
if (rlang::quo_is_call(x, "c") || rlang::quo_is_call(x, "list")) {
return(rlang::as_quosures(rlang::get_expr(x)[-1], rlang::get_env(x)))
}
x
}