diff --git a/NEWS.md b/NEWS.md index cf962be..32fbbcd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/xlsx_cutter.R b/R/xlsx_cutter.R index d4c4a2d..e2b596a 100644 --- a/R/xlsx_cutter.R +++ b/R/xlsx_cutter.R @@ -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) diff --git a/tests/testthat/_snaps/xlsx_cutter.md b/tests/testthat/_snaps/xlsx_cutter.md index a302d27..7c5aac1 100644 --- a/tests/testthat/_snaps/xlsx_cutter.md +++ b/tests/testthat/_snaps/xlsx_cutter.md @@ -63,3 +63,9 @@ ] } +# failures don't stop loop + + parsing failed for 2 files: + - nonexistent_file.xlsx + - nonexistent_file2.xlsx + diff --git a/tests/testthat/test-xlsx_cutter.R b/tests/testthat/test-xlsx_cutter.R index c4958b2..92bf330 100644 --- a/tests/testthat/test-xlsx_cutter.R +++ b/tests/testthat/test-xlsx_cutter.R @@ -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)) + +})