Skip to content

Commit

Permalink
Added dynamic argument checking for 'readODS:read_ods()' (gesistsa#225)
Browse files Browse the repository at this point in the history
* Added dynamic argument checking for 'readODS:read_ods()' as proposed in gesistsa#223. Also more detailed tests.
  • Loading branch information
bokov authored and leeper committed Oct 3, 2019
1 parent b9f78fa commit f3b3c2d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
24 changes: 23 additions & 1 deletion R/import_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,29 @@ function(file,
#' @export
.import.rio_ods <- function(file, which = 1, header = TRUE, ...) {
requireNamespace("readODS")
readODS::read_ods(path = file, sheet = which, col_names = header, ...)
"read_ods" <- readODS::read_ods
a <- list(...)
if ("sheet" %in% names(a)) {
which <- a[["sheet"]]
a[["sheet"]] <- NULL
}
if ("col_names" %in% names(a)) {
header <- a[["col_names"]]
a[["col_names"]] <- NULL
}
frml <- formals(readODS::read_ods)
unused <- setdiff(names(a), names(frml))
if ("path" %in% names(a)) {
unused <- c(unused, 'path')
a[["path"]] <- NULL
}
if (length(unused)>0) {
warning("The following arguments were ignored for read_ods:\n",
paste(unused, collapse = ', '))
}
a <- a[intersect(names(a), names(frml))]
do.call("read_ods",
c(list(path = file, sheet = which, col_names = header),a))
}

#' @importFrom utils type.convert
Expand Down
14 changes: 13 additions & 1 deletion tests/testthat/test_format_ods.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ require("datasets")

test_that("Import from ODS", {
skip_if_not_installed(pkg="readODS")
ods <- import(system.file("examples", "mtcars.ods", package = "rio"))
expect_message(ods0 <- import(system.file("examples", "mtcars.ods",
package = "rio")),
'Parsed with column specification:',
label = "ODS import with default arguments works")
expect_warning(ods <- import(system.file("examples", "mtcars.ods"
, package = "rio"),
sheet = 1, col_names = TRUE,
path = 'ignored value',
invalid_argument = 42),
"The following arguments were ignored for read_ods:\ninvalid_argument, path",
label = "ODS import ignores redundant and unknown arguments with a warning")
expect_identical(ods0,ods, label = "ODS import ignored arguments don't affect output")
expect_true(is.data.frame(ods), label = "ODS import returns data.frame")
expect_true(identical(names(mtcars), names(ods)), label = "ODS import returns correct names")
expect_true(identical(dim(mtcars), dim(ods)), label = "ODS import returns correct dimensions")
expect_equivalent(ods,mtcars,label = "ODS import returns correct values")
})

test_that("Export to ODS", {
Expand Down

0 comments on commit f3b3c2d

Please sign in to comment.