From 8d11625f16f288f0c10c6928bd98039b07c9e42d Mon Sep 17 00:00:00 2001 From: Jan Marvin Garbuszus Date: Thu, 17 Aug 2023 18:31:41 +0200 Subject: [PATCH] guard against empty cc cases --- R/read.R | 5 +++++ tests/testthat/test-wb_functions.R | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/R/read.R b/R/read.R index 7717c07fa..9693f1d17 100644 --- a/R/read.R +++ b/R/read.R @@ -312,6 +312,11 @@ wb_to_df <- function( if (!is.null(cols) && !is.null(rows) && !missing(dims)) cc <- cc[cc$row_r %in% keep_rows & cc$c_r %in% keep_cols, ] + # guard against special case where no col/row is part of cc + if (NROW(cc) == 0) { + cc[1, ] <- rep("", ncol(cc)) + } + cc$val <- NA_character_ cc$typ <- NA_character_ diff --git a/tests/testthat/test-wb_functions.R b/tests/testthat/test-wb_functions.R index d717e03a3..c4352d96f 100644 --- a/tests/testthat/test-wb_functions.R +++ b/tests/testthat/test-wb_functions.R @@ -304,3 +304,14 @@ test_that("cols return order is correct", { expect_equal(exp, got) }) + +test_that("missings cells are returned", { + + wb <- wb_workbook()$add_worksheet()$add_data(x = "a", dims = "Z100") + + expect_silent(got <- wb_to_df(wb, col_names = FALSE, rows = 1, cols = 1)) + expect_silent(got <- wb_to_df(wb, col_names = FALSE, rows = c(1, 4, 10), cols = c(2, 5, 10))) + + expect_silent(got <- wb_to_df(wb, col_names = FALSE, rows = c(101), cols = c(27))) + +})