Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rio 1.2.4

* Fix #463, export_list also allows ods and fods

Bug fixes

* Fix #458, custom S3 import and export functions work again
Expand Down
11 changes: 8 additions & 3 deletions R/import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import_list <- function(file, setclass = getOption("rio.import.class", "data.fra
return(.import.rio_rdata(file = file, .return_everything = TRUE, ...))
}
archive_format <- find_compress(file)
if (!get_info(file)$format %in% c("html", "xlsx", "xls") && !archive_format$compress %in% c("zip", "tar", "tar.gz", "tar.bz2")) {
if (!get_info(file)$format %in% c("html", "xlsx", "xls", "ods", "fods") && !archive_format$compress %in% c("zip", "tar", "tar.gz", "tar.bz2")) {
which <- 1
whichnames <- NULL
}
Expand All @@ -106,9 +106,14 @@ import_list <- function(file, setclass = getOption("rio.import.class", "data.fra
)
names(which) <- whichnames
}
if (get_info(file)$format %in% c("xls", "xlsx")) {
if (get_info(file)$format %in% c("xls", "xlsx", "ods", "fods")) {
## .check_pkg_availability("readxl")
whichnames <- readxl::excel_sheets(path = file)
sheet_func <- readxl::excel_sheets
if (get_info(file)$format %in% c("ods", "fods")) {
.check_pkg_availability("readODS")
sheet_func <- readODS::list_ods_sheets
}
whichnames <- sheet_func(path = file)
if (missing(which)) {
which <- seq_along(whichnames)
names(which) <- whichnames
Expand Down
30 changes: 29 additions & 1 deletion tests/testthat/test_import_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ test_that("import_list() preserves 'which' names when specified", {
})
})

test_that("import_list() preserves 'which' names when specified ods", {
skip_if_not_installed("readODS")
withr::with_tempfile("data_file", fileext = ".ods", code = {
export(list(a = mtcars, b = iris), data_file)
expect_true(identical(names(import_list(data_file)), c("a", "b")))
expect_true(identical(names(import_list(data_file, which = 1)), "a"))
expect_true(identical(names(import_list(data_file, which = "a")), "a"))
expect_true(identical(names(import_list(data_file, which = 2)), "b"))
expect_true(identical(names(import_list(data_file, which = "b")), "b"))
expect_true(identical(names(import_list(data_file, which = 1:2)), c("a", "b")))
expect_true(identical(names(import_list(data_file, which = 2:1)), c("b", "a")))
expect_true(identical(names(import_list(data_file, which = c("a", "b"))), c("a", "b")))
expect_true(identical(names(import_list(data_file, which = c("b", "a"))), c("b", "a")))
})
})


test_that("Import single file via import_list()", {
withr::with_tempfile("data_file", fileext = ".rds", code = {
export(mtcars, data_file)
Expand Down Expand Up @@ -113,14 +130,24 @@ test_that("Object names are preserved by import_list()", {
export(list(mtcars1 = mtcars[1:10,],
mtcars2 = mtcars[11:20,],
mtcars3 = mtcars[21:32,]), "mtcars.xlsx")
export(list(mtcars1 = mtcars[1:10,],
mtcars2 = mtcars[11:20,],
mtcars3 = mtcars[21:32,]), "mtcars.ods")
export(list(mtcars1 = mtcars[1:10,],
mtcars2 = mtcars[11:20,],
mtcars3 = mtcars[21:32,]), "mtcars.fods")
export(mtcars[1:10,], "mtcars1.csv")
export(mtcars[11:20,], "mtcars2.tsv")
export(mtcars[21:32,], "mtcars3.csv")
expected_names <- c("mtcars1", "mtcars2", "mtcars3")
dat_xls <- import_list("mtcars.xlsx")
dat_csv <- import_list(c("mtcars1.csv","mtcars2.tsv","mtcars3.csv"))
dat_ods <- import_list("mtcars.ods")
dat_fods <- import_list("mtcars.fods")
expect_identical(names(dat_xls), expected_names)
expect_identical(names(dat_csv), expected_names)
expect_identical(names(dat_ods), expected_names)
expect_identical(names(dat_fods), expected_names)
})
})

Expand Down Expand Up @@ -154,7 +181,8 @@ test_that("URL #294", {
})

test_that("Universal dummy `which` #326", {
formats <- c("xlsx", "dta", "sav", "csv", "csv2")
skip_if_not_installed("readODS")
formats <- c("ods", "fods", "xlsx", "dta", "sav", "csv", "csv2")
for (format in formats) {
withr::with_tempfile("tempzip", fileext = paste0(".", format, ".zip"), code = {
rio::export(mtcars, tempzip, format = format)
Expand Down