Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dynamic argument checking for 'readODS:read_ods()' #225

Merged
merged 2 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion R/import_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,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:",
bokov marked this conversation as resolved.
Show resolved Hide resolved
"\n", paste(unused, collapse = ', '))
}
a <- a[intersect(names(a), names(frml))]
do.call("read_ods"
bokov marked this conversation as resolved.
Show resolved Hide resolved
,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