Skip to content

Commit

Permalink
Allow reading list of connections (#514)
Browse files Browse the repository at this point in the history
* Allow reading list of connections

* Use sapply dots

* Standardise single connection

* Add NEWS bullet

---------

Co-authored-by: Jenny Bryan <jenny.f.bryan@gmail.com>
  • Loading branch information
bairdj and jennybc committed Sep 26, 2023
1 parent ea00a1f commit 6a320a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vroom (development version)

* It is now possible (again?) to read from a list of connections (@bairdj, #514).

* Internal change for compatibility with cpp11 >= 0.4.6 (@DavisVaughan).

# vroom 1.6.3
Expand Down
22 changes: 15 additions & 7 deletions R/path.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ standardise_path <- function(path, user_env = caller_env(2)) {
}

if (inherits(path, "connection")) {
# If the connection is `stdin()`, change it to `file("stdin")`, as we don't
# support text mode connections.

if (path == stdin()) {
return(list(file("stdin")))
}
return(list(standardise_connection(path)))
}

return(list(path))
if (is_list(path) && all(sapply(path, inherits, "connection"))) {
return(lapply(path, standardise_connection))
}

if (is.character(path)) {
Expand Down Expand Up @@ -66,6 +63,17 @@ standardise_path <- function(path, user_env = caller_env(2)) {
as.list(enc2utf8(path))
}

standardise_connection <- function(con) {
# If the connection is `stdin()`, change it to `file("stdin")`, as we don't
# support text mode connections.

if (con == stdin()) {
return(file("stdin"))
}

con
}

standardise_one_path <- function (path, write = FALSE) {

if (is.raw(path)) {
Expand Down
7 changes: 4 additions & 3 deletions tests/testthat/test-multi-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test_that("vroom works with many connections", {
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE))

for (i in seq_len(200)) {
for (i in seq_len(20)) {
vroom_write(
tibble::tibble(
x = rnorm(10),
Expand All @@ -87,11 +87,12 @@ test_that("vroom works with many connections", {
}

files <- list.files(dir, pattern = ".*[.]csv[.]gz", full.names = TRUE)
connections <- lapply(files, gzfile)

res <- vroom::vroom(files, col_types = list())
res <- vroom::vroom(connections, col_types = list())

expect_equal(colnames(res), c("x", "y"))
expect_equal(NROW(res), 2000)
expect_equal(NROW(res), 200)
})

test_that("vroom errors if numbers of columns are inconsistent", {
Expand Down

0 comments on commit 6a320a7

Please sign in to comment.