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

Add tryCatch to loop #13

Merged
merged 3 commits into from
Feb 10, 2024
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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# xlcutter (development version)

* `xlsx_cutter()` internal loop won't be interrupted in case a file can't be
parsed. This avoid loosing potentially hours of computation when one of the
last files fails and made the entire function crash. All parsing issues are
now returned as warnings at the end (#13, @Bisaloo).

# xlcutter 0.1.1

* `xlsx_cutter()` no longer fails when reading excel files with comments on
Expand Down
19 changes: 17 additions & 2 deletions R/xlsx_cutter.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,25 @@ xlsx_cutter <- function(

res <- lapply(
data_files,
single_xlsx_cutter,
template_file, data_sheet, coords, noms
function(f) {
tryCatch(
single_xlsx_cutter(f, template_file, data_sheet, coords, noms),
warning = function(c) NULL,
error = function(c) NULL
)
}
)

failed <- vapply(res, is.null, logical(1))

if (any(failed)) {
warning(
"parsing failed for ", sum(failed), " files:\n - ",
paste(data_files[failed], collapse = "\n - "),
call. = FALSE
)
}

res <- as.data.frame(do.call(rbind, res))

type.convert(res, as.is = TRUE)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/_snaps/xlsx_cutter.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@
]
}

# failures don't stop loop

parsing failed for 2 files:
- nonexistent_file.xlsx
- nonexistent_file2.xlsx

13 changes: 13 additions & 0 deletions tests/testthat/test-xlsx_cutter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ test_that("xlsx_cutter() works", {
)

})

test_that("failures don't stop loop", {

expect_snapshot_warning(
res <- xlsx_cutter( # nolintr: assignment_linter.
c("nonexistent_file.xlsx", "nonexistent_file2.xlsx", data_files),
template_file
)
)

expect_identical(nrow(res), length(data_files))

})
Loading