Skip to content

Commit a2b34a3

Browse files
authored
Fix weirdness with collector_value methods for number and skip cols (#573)
* Fix weirdness with collector_value methods for number and skip cols * Add NEWS bullet
1 parent dd6264c commit a2b34a3

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ S3method(collector_value,collector_factor)
1313
S3method(collector_value,collector_guess)
1414
S3method(collector_value,collector_integer)
1515
S3method(collector_value,collector_logical)
16-
S3method(collector_value,collector_numeric)
16+
S3method(collector_value,collector_number)
17+
S3method(collector_value,collector_skip)
1718
S3method(collector_value,collector_time)
1819
S3method(output_column,POSIXt)
1920
S3method(output_column,character)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# vroom (development version)
22

3+
* Columns specified as having type "number" (requested via `col_number()` or `"number"` or `'n'`) or "skip" (requested via `col_skip()` or `"skip"` or `_` or `-`) now work in the case where 0 rows of data are parsed (#427, #540, #548).
4+
35
# vroom 1.6.7
46

57
* `locale(encoding =)` now warns, instead of errors, when the encoding cannot be found in `iconvlist()` return value. This removes an unnecessary blocker on platforms like Alpine Linux where the output doesn't reflect actual capabilities.

R/col_types.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ collector_value.collector_integer <- function(x, ...) {
569569
}
570570

571571
#' @export
572-
collector_value.collector_numeric <- function(x, ...) {
572+
collector_value.collector_number <- function(x, ...) {
573573
numeric()
574574
}
575575

@@ -607,6 +607,11 @@ collector_value.collector_guess <- function(x, ...) {
607607
character()
608608
}
609609

610+
#' @export
611+
collector_value.collector_skip <- function(x, ...) {
612+
NULL
613+
}
614+
610615
#' @export
611616
summary.col_spec <- function(
612617
object,

R/vroom.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,24 @@ vroom <- function(
282282
progress = progress
283283
)
284284

285-
# If no rows expand columns to be the same length and names as the spec
285+
# If no rows, expand columns to be the same length and names as the spec
286+
# Skipped columns present a bit of a wrinkle: they appear in the spec,
287+
# but not in the result
286288
if (NROW(out) == 0) {
287289
cols <- attr(out, "spec")[["cols"]]
288-
for (i in seq_along(cols)) {
289-
out[[i]] <- collector_value(cols[[i]])
290+
nms <- names(cols)
291+
292+
out_i <- 1
293+
for (cols_i in seq_along(cols)) {
294+
value <- collector_value(cols[[cols_i]])
295+
if (is.null(value)) {
296+
# this is a skipped column
297+
next
298+
}
299+
out[[out_i]] <- value
300+
names(out)[out_i] <- nms[cols_i]
301+
out_i <- out_i + 1
290302
}
291-
names(out) <- names(cols)
292303
}
293304

294305
out <- tibble::as_tibble(out, .name_repair = identity)

tests/testthat/test-col_types.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@ test_that("all col_types can be reported with color", {
3333
)
3434
expect_snapshot(spec(dat))
3535
})
36+
37+
# https://github.com/tidyverse/vroom/issues/548
38+
test_that("col_number() works with empty data", {
39+
out <- vroom(I(""), col_types = "in")
40+
expect_equal(out, tibble::tibble(X1 = integer(), X2 = numeric()))
41+
})
42+
43+
# https://github.com/tidyverse/vroom/issues/540
44+
test_that("col_skip works with empty data (#540)", {
45+
out <- vroom(I("a\tb\tc\n"), delim = "\t", col_types = "c-d", skip = 1L)
46+
expect_equal(out, tibble::tibble(X1 = character(), X3 = numeric()))
47+
48+
# All columns skipped
49+
out <- vroom(I("a\tb\n"), delim = "\t", col_types = "--", skip = 1L)
50+
expect_equal(out, tibble::tibble())
51+
})

0 commit comments

Comments
 (0)