Skip to content

Commit

Permalink
Merge pull request #372 from jennybc/too-many-col-types
Browse files Browse the repository at this point in the history
Subset col_types when there are too many; fixes #371
  • Loading branch information
jimhester committed Jun 15, 2016
2 parents 3b677d5 + 2fb8e17 commit 1e00465
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

* Negative column widths are now allowed in `fwf_widths()` to facilitate
compatibility with the `widths` argument in `read.fwf()`. (#380, @leeper)
* `type_covert()` now accepts only `NULL` or a `cols` specification for
* `type_convert()` now accepts only `NULL` or a `cols` specification for
`col_types` (#369, @jimhester).
* If `col_types` is too long, it is subsetted correctly. (#372, @jennybc)
* `read_file()`, `read_lines()` and `read_csv()` now return empty objects
rather than signaling an error when run on an empty file (#356, @jimhester).

Expand Down
2 changes: 1 addition & 1 deletion R/col_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ col_spec_standardise <- function(file, col_names = TRUE, col_types = NULL,
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(col_types))
n <- min(length(col_names), length(spec$cols))
spec$cols <- spec$cols[seq_len(n)]
col_names <- col_names[seq_len(n)]
}
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-read-csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ test_that("extra columns generates warnings", {
expect_equal(problems(out4)$expected, "2 columns")
})

test_that("too few or extra col_types generates warnings", {
expect_warning(out1 <- read_csv("v1,v2\n1,2", col_types = "i"))
expect_equal(problems(out1)$expected, "1 columns")
expect_equal( problems(out1)$actual, "2 columns")

expect_warning(out2 <- read_csv("v1,v2\n1,2", col_types = "iii"))
expect_equal(ncol(out2), 2)
})

# read_csv2 ---------------------------------------------------------------

test_that("decimal mark automatically set to ,", {
Expand Down

0 comments on commit 1e00465

Please sign in to comment.