Skip to content

Commit

Permalink
Add tryCatch to loop (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bisaloo authored Feb 10, 2024
1 parent 8dd3833 commit e111281
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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))

})

0 comments on commit e111281

Please sign in to comment.