Skip to content

Commit 65b3e3f

Browse files
authored
Respect the format argument to import_list() (fix #467) (#468)
* Add failing test * Respect the `format` argument to `import_list()` (fix #467)
1 parent e14dfc3 commit 65b3e3f

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

R/import_list.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,25 @@ import_list <- function(file, setclass = getOption("rio.import.class", "data.fra
8282
}
8383

8484
.read_file_as_list <- function(file, which, setclass, rbind, rbind_label, ...) {
85+
## respect the `format` argument, if given
86+
if ("format" %in% ...names()) {
87+
format <- list(...)[["format"]]
88+
} else {
89+
format <- get_info(file)$format
90+
}
8591
if (R.utils::isUrl(file)) {
8692
file <- remote_to_local(file)
8793
}
88-
if (get_info(file)$format == "rdata") {
94+
if (format == "rdata") {
8995
return(.import.rio_rdata(file = file, .return_everything = TRUE, ...))
9096
}
9197
archive_format <- find_compress(file)
92-
if (!get_info(file)$format %in% c("html", "xlsx", "xls", "ods", "fods") && !archive_format$compress %in% c("zip", "tar", "tar.gz", "tar.bz2")) {
98+
if (!format %in% c("html", "xlsx", "xls", "ods", "fods") && !archive_format$compress %in% c("zip", "tar", "tar.gz", "tar.bz2")) {
9399
which <- 1
94100
whichnames <- NULL
95101
}
96102
## getting list of `whichnames`
97-
if (get_info(file)$format == "html") {
103+
if (format == "html") {
98104
.check_pkg_availability("xml2")
99105
tables <- xml2::xml_find_all(xml2::read_html(unclass(file)), ".//table")
100106
if (missing(which)) {
@@ -106,10 +112,10 @@ import_list <- function(file, setclass = getOption("rio.import.class", "data.fra
106112
)
107113
names(which) <- whichnames
108114
}
109-
if (get_info(file)$format %in% c("xls", "xlsx", "ods", "fods")) {
115+
if (format %in% c("xls", "xlsx", "ods", "fods")) {
110116
## .check_pkg_availability("readxl")
111117
sheet_func <- readxl::excel_sheets
112-
if (get_info(file)$format %in% c("ods", "fods")) {
118+
if (format %in% c("ods", "fods")) {
113119
.check_pkg_availability("readODS")
114120
sheet_func <- readODS::list_ods_sheets
115121
}

tests/testdata/example.xlsm

9.38 KB
Binary file not shown.

tests/testthat/test_format_xls.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ test_that("Import from Excel (.xls)", {
4949
expect_true(is.data.frame(import("../testdata/iris.xls", sheet = 1)))
5050
expect_true(is.data.frame(import("../testdata/iris.xls", which = 1)))
5151
})
52+
53+
test_that("Import from Excel (.xlsm)", {
54+
expect_true(is.data.frame(import("../testdata/example.xlsm", sheet = 1, format = "xlsx")))
55+
expect_true(is.data.frame(import("../testdata/example.xlsm", which = 1, format = "xlsx")))
56+
expect_equal(
57+
import_list("../testdata/example.xlsm", format = "xlsx"),
58+
list(
59+
Sheet1 = data.frame(A = 1),
60+
Sheet2 = data.frame(B = 2)
61+
)
62+
)
63+
})

0 commit comments

Comments
 (0)