Skip to content

Commit

Permalink
Fix vroom_lines to respect n_max
Browse files Browse the repository at this point in the history
Previously delim == '\n', which meant that the file had only delimiters
and no newlines, so n_max was never decremented internally. Switching
the delimiter to '\1', which should never appear in a file fixes this.

Fixes #142
  • Loading branch information
jimhester committed Jun 25, 2019
1 parent 9ba6eb3 commit d9cdd79
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
@@ -1,5 +1,7 @@
# vroom (development)

* `vroom_lines()` now properly respects the `n_max` parameter (#142)

* The integer parser now returns NA values for invalid inputs (#135)

* The column created by `id` is now stored as an run length encoded Altrep
Expand Down
6 changes: 5 additions & 1 deletion R/vroom_lines.R
Expand Up @@ -27,7 +27,11 @@ vroom_lines <- function(file, n_max = Inf, skip = 0, altrep_opts = "chr",

col_select <- rlang::quo(NULL)

out <- vroom_(file, delim = "\n", col_names = "V1", col_types = "c",
# delim = "\1" sets the delimiter to be start of header, which should never
# appear in modern text. This essentially means the only record breaks will
# be newlines. Ideally this would be "\0", but R doesn't let you have nulls
# in character vectors.
out <- vroom_(file, delim = "\1", col_names = "V1", col_types = "c",
id = NULL, skip = skip, col_select = col_select, na = character(), quote = "",
trim_ws = FALSE, escape_double = FALSE, escape_backslash = FALSE, comment = "",
locale = default_locale(),
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-vroom_lines.R
Expand Up @@ -46,3 +46,8 @@ test_that("vroom_lines works with files with no trailing newline", {
writeBin(charToRaw("foo\nbar"), f2)
expect_equal(vroom_lines(f2), c("foo", "bar"))
})

test_that("vroom_lines respects n_max", {
infile <- vroom_example("mtcars.csv")
expect_equal(vroom_lines(infile, n_max = 2), readLines(infile, n = 2))
})

0 comments on commit d9cdd79

Please sign in to comment.