/
col_types.R
373 lines (312 loc) · 10.6 KB
/
col_types.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#' Create column specification
#'
#' @param ... Either column objects created by `col_*()`, or their
#' abbreviated character names. If you're only overriding a few columns,
#' it's best to refer to columns by name. If not named, the column types
#' must match the column names exactly.
#' @param .default Any named columns not explicitly overridden in `...`
#' will be read with this column type.
#' @export
#' @examples
#' cols(a = col_integer())
#' cols_only(a = col_integer())
#'
#' # You can also use the standard abreviations
#' cols(a = "i")
#' cols(a = "i", b = "d", c = "_")
cols <- function(..., .default = col_guess()) {
col_types <- list(...)
is_character <- vapply(col_types, is.character, logical(1))
col_types[is_character] <- lapply(col_types[is_character], col_concise)
if (is.character(.default)) {
.default <- col_concise(.default)
}
col_spec(col_types, .default)
}
#' @export
#' @rdname cols
cols_only <- function(...) {
cols(..., .default = col_skip())
}
# col_spec ----------------------------------------------------------------
col_spec <- function(col_types, default = col_guess()) {
stopifnot(is.list(col_types))
stopifnot(is.collector(default))
is_collector <- vapply(col_types, is.collector, logical(1))
if (any(!is_collector)) {
stop("Some `col_types` are not S3 collector objects: ",
paste(which(!is_collector), collapse = ", "), call. = FALSE)
}
structure(
list(
cols = col_types,
default = default
),
class = "col_spec"
)
}
is.col_spec <- function(x) inherits(x, "col_spec")
as.col_spec <- function(x) UseMethod("as.col_spec")
#' @export
as.col_spec.character <- function(x) {
letters <- strsplit(x, "")[[1]]
col_spec(lapply(letters, col_concise), col_guess())
}
#' @export
as.col_spec.NULL <- function(x) {
col_spec(list())
}
#' @export
as.col_spec.list <- function(x) {
do.call(cols, x)
}
#' @export
as.col_spec.col_spec <- function(x) x
#' @export
as.col_spec.default <- function(x) {
stop("`col_types` must be NULL, a list or a string", call. = FALSE)
}
#' @export
print.col_spec <- function(x, n = Inf, condense = NULL, ...) {
cat(format.col_spec(x, n = n, condense = condense, ...))
invisible(x)
}
#' @description
#' `cols_condense()` takes a spec object and condenses its definition by setting
#' the default column type to the most frequent type and only listing columns
#' with a different type.
#' @rdname spec
#' @export
cols_condense <- function(x) {
types <- vapply(x$cols, function(xx) class(xx)[[1]], character(1))
counts <- table(types)
most_common <- names(counts)[counts == max(counts)][[1]]
x$default <- x$cols[types == most_common][[1]]
x$cols <- x$cols[types != most_common]
x
}
#' @export
format.col_spec <- function(x, n = Inf, condense = NULL, ...) {
if (n == 0) {
return("")
}
# condense if cols >= n
condense <- condense %||% (length(x$cols) >= n)
if (isTRUE(condense)) {
x <- cols_condense(x)
}
# truncate to minumum of n or length
cols <- x$cols[seq_len(min(length(x$cols), n))]
default <- NULL
if (inherits(x$default, "collector_guess")) {
fun_type <- "cols"
} else if (inherits(x$default, "collector_skip")) {
fun_type <- "cols_only"
} else {
fun_type <- "cols"
type <- sub("^collector_", "", class(x$default)[[1]])
default <- paste0(".default = col_", type, "()")
}
cols_args <- c(default,
vapply(seq_along(cols),
function(i) {
col_funs <- sub("^collector_", "col_", class(cols[[i]])[[1]])
args <- vapply(cols[[i]], deparse2, character(1), sep = "\n ")
args <- paste(names(args), args, sep = " = ", collapse = ", ")
col_names <- names(cols)[[i]]
# Need to handle unnamed columns and columns with non-syntactic names
named <- col_names != ""
non_syntactic <- !is_syntactic(col_names) & named
col_names[non_syntactic] <- paste0("`", gsub("`", "\\\\`", col_names[non_syntactic]), "`")
out <- paste0(col_names, " = ", col_funs, "(", args, ")")
out[!named] <- paste0(col_funs, "(", args, ")")
out
},
character(1)
)
)
if (length(x$cols) == 0 && length(cols_args) == 0) {
return(paste0(fun_type, "()\n"))
}
out <- paste0(fun_type, "(\n ", paste(collapse = ",\n ", cols_args))
if (length(x$cols) > n) {
out <- paste0(out, "\n # ... with ", length(x$cols) - n, " more columns")
}
out <- paste0(out, "\n)\n")
out
}
# Used in read_delim(), read_fwf() and type_convert()
show_cols_spec <- function(spec, n = getOption("readr.num_columns", 20)) {
if (n > 0) {
message("Parsed with column specification:\n",
format(spec, n = n, condense = NULL), appendLF = FALSE)
if (length(spec$cols) >= n) {
message("See spec(...) for full column specifications.")
}
}
}
#' Examine the column specifications for a data frame
#'
#' `spec()` extracts the full column specification from a tibble
#' created by readr.
#'
#' @param x The data frame object to extract from
#' @return A col_spec object.
#' @export
#' @examples
#' df <- read_csv(readr_example("mtcars.csv"))
#' s <- spec(df)
#' s
#'
#' cols_condense(s)
spec <- function(x) {
stopifnot(inherits(x, "tbl_df"))
attr(x, "spec")
}
col_concise <- function(x) {
switch(x,
"_" = ,
"-" = col_skip(),
"?" = col_guess(),
c = col_character(),
D = col_date(),
d = col_double(),
i = col_integer(),
l = col_logical(),
n = col_number(),
T = col_datetime(),
t = col_time(),
stop("Unknown shortcut: ", x, call. = FALSE)
)
}
col_spec_standardise <- function(file, col_names = TRUE, col_types = NULL,
guessed_types = NULL,
comment = "", skip = 0, n = 1000,
tokenizer = tokenizer_csv(),
locale = default_locale(),
drop_skipped_names = FALSE) {
# Figure out the column names -----------------------------------------------
if (is.logical(col_names) && length(col_names) == 1) {
ds_header <- datasource(file, skip = skip, comment = comment)
if (col_names) {
col_names <- guess_header(ds_header, tokenizer, locale)
skip <- skip + 1
} else {
n <- length(guess_header(ds_header, tokenizer, locale))
col_names <- paste0("X", seq_len(n))
}
guessed_names <- TRUE
} else if (is.character(col_names)) {
guessed_names <- FALSE
} else {
stop("`col_names` must be TRUE, FALSE or a character vector", call. = FALSE)
}
missing_names <- is.na(col_names)
if (any(missing_names)) {
new_names <- paste0("X", seq_along(col_names)[missing_names])
col_names[missing_names] <- new_names
warning(
"Missing column names filled in: ",
paste0(
encodeString(new_names, quote = "'"),
" [", which(missing_names), "]",
collapse = ", "
), call. = FALSE
)
}
if (anyDuplicated(col_names)) {
dups <- duplicated(col_names)
old_names <- col_names
col_names <- make.unique(col_names, sep = "_")
warning(
"Duplicated column names deduplicated: ",
paste0(
encodeString(old_names[dups], quote = "'"),
" => ",
encodeString(col_names[dups], quote = "'"),
" [", which(dups), "]",
collapse = ", "
), call. = FALSE
)
}
# Figure out column types ----------------------------------------------------
spec <- as.col_spec(col_types)
type_names <- names(spec$cols)
if (length(spec$cols) == 0) {
# no types specified so use defaults
spec$cols <- rep(list(spec$default), length(col_names))
names(spec$cols) <- col_names
} else if (is.null(type_names) && guessed_names) {
# unnamed types & names guessed from header: match exactly
if (length(spec$cols) != length(col_names)) {
warning("Unnamed `col_types` should have the same length as `col_names`. ",
"Using smaller of the two.", call. = FALSE)
n <- min(length(col_names), length(spec$cols))
spec$cols <- spec$cols[seq_len(n)]
col_names <- col_names[seq_len(n)]
}
names(spec$cols) <- col_names
} else if (is.null(type_names) && !guessed_names) {
# unnamed types & names supplied: match non-skipped columns
skipped <- vapply(spec$cols, inherits, "collector_skip",
FUN.VALUE = logical(1))
# Needed for read_fwf() because width generator functions have name for
# every column, even those that are skipped. Not need for read_delim()
if (drop_skipped_names) {
col_names <- col_names[!skipped]
}
n_read <- sum(!skipped)
n_names <- length(col_names)
n_new <- abs(n_names - n_read)
if (n_read < n_names) {
warning("Insufficient `col_types`. Guessing ", n_new, " columns.",
call. = FALSE)
spec$cols <- c(spec$cols, list(rep(col_guess(), n_new)))
} else if (n_read > n_names) {
warning("Insufficient `col_names`. Adding ", n_new, " names.",
call. = FALSE)
col_names2 <- rep("", length(spec$cols))
col_names2[!skipped] <- c(col_names, paste0("X", seq_len(n_new) + n_names))
col_names <- col_names2
} else {
col_names2 <- rep("", length(spec$cols))
col_names2[!skipped] <- col_names
col_names <- col_names2
}
names(spec$cols) <- col_names
} else {
# names types
bad_types <- !(type_names %in% col_names)
if (any(bad_types)) {
warning("The following named parsers don't match the column names: ",
paste0(type_names[bad_types], collapse = ", "), call. = FALSE)
spec$cols <- spec$cols[!bad_types]
type_names <- type_names[!bad_types]
}
default_types <- !(col_names %in% type_names)
if (any(default_types)) {
defaults <- rep(list(spec$default), sum(default_types))
names(defaults) <- col_names[default_types]
spec$cols[names(defaults)] <- defaults
}
spec$cols <- spec$cols[col_names]
}
# Guess any types that need to be guessed ------------------------------------
is_guess <- vapply(spec$cols, function(x) inherits(x, "collector_guess"), logical(1))
if (any(is_guess)) {
if (is.null(guessed_types)) {
ds <- datasource(file, skip = skip, comment = comment)
guessed_types <- guess_types(ds, tokenizer, locale, n = n)
}
# Need to be careful here: there might be more guesses than types/names
guesses <- guessed_types[seq_along(spec$cols)][is_guess]
spec$cols[is_guess] <- lapply(guesses, collector_find)
}
spec
}
guess_types <- function(datasource, tokenizer, locale, n = 1000) {
guess_types_(datasource, tokenizer, locale, n = n)
}
guess_header <- function(datasource, tokenizer, locale = default_locale()) {
guess_header_(datasource, tokenizer, locale)
}