Skip to content

Commit

Permalink
Use a datasource for all file arguments in read_table
Browse files Browse the repository at this point in the history
Fixes #552
  • Loading branch information
jimhester committed Feb 6, 2017
1 parent be93d4c commit ff564d0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# readr 1.0.0.9000

* `read_table()` can now handle `pipe()` connections (#552).

* parsing problems in `read_delim()` and `read_fwf()` when columns are skipped using col_types now report the correct column name (#573, @cb4ds)

* `parse_time()` now correctly handles 12 AM/PM (#579).
Expand Down
7 changes: 4 additions & 3 deletions R/read_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ read_table <- function(file, col_names = TRUE, col_types = NULL,
locale = default_locale(), na = "NA", skip = 0,
n_max = Inf, guess_max = min(n_max, 1000),
progress = show_progress(), comment = "") {
columns <- fwf_empty(file, skip = skip, n = guess_max, comment = comment)
ds <- datasource(file, skip = skip)
columns <- fwf_empty(ds, skip = skip, n = guess_max, comment = comment)
tokenizer <- tokenizer_fwf(columns$begin, columns$end, na = na, comment = comment)

spec <- col_spec_standardise(
file = file, skip = skip, n = guess_max,
file = ds, skip = skip, n = guess_max,
col_names = col_names, col_types = col_types,
locale = locale, tokenizer = tokenizer
)
Expand All @@ -43,7 +44,7 @@ read_table <- function(file, col_names = TRUE, col_types = NULL,
print(spec, n = getOption("readr.num_columns", 20))
}

ds <- datasource(file, skip = skip + isTRUE(col_names))
ds <- datasource(file = ds, skip = skip + isTRUE(col_names))
res <- read_tokens(ds, tokenizer, spec$cols, names(spec$cols), locale_ = locale,
n_max = n_max, progress = progress)
attr(res, "spec") <- spec
Expand Down
2 changes: 2 additions & 0 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#' close(con)
datasource <- function(file, skip = 0, comment = "") {
if (inherits(file, "source")) {
if (!missing(skip)) { file$skip <- skip }
if (!missing(comment)) { file$comment <- comment }
file
} else if (is.connection(file)) {
datasource_connection(file, skip, comment)
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-read-fwf.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ test_that("read_table silently reads ragged last column", {
x <- read_table("foo bar\n1 2\n3 4\n5 6\n", progress = FALSE)
expect_equal(x$foo, c(1, 3, 5))
})

test_that("read_table can read from a pipe (552)", {
x <- read_table(pipe("echo a b c && echo 1 2 3 && echo 4 5 6"))
expect_equal(x$a, c(1, 4))
})

0 comments on commit ff564d0

Please sign in to comment.