diff --git a/NEWS.md b/NEWS.md index b1add965a..695695f23 100644 --- a/NEWS.md +++ b/NEWS.md @@ -20,6 +20,8 @@ * Column style `currency` is now correctly applied to numeric vectors. Previously it was not handled. This applies the built in spreadsheet style for currency presumably linked to the spreadsheet software locale. [879](https://github.com/JanMarvin/openxlsx2/pull/879) +* `wb_to_df(col_names = FALSE)` no longer drops column names from logical vectors. Previously, column names were replaced by `NA`. Now the column name is returned as a cell value in a character column. [884](https://github.com/JanMarvin/openxlsx2/pull/884) + *************************************************************************** diff --git a/R/wb_functions.R b/R/wb_functions.R index ac075a821..c28ba9eb1 100644 --- a/R/wb_functions.R +++ b/R/wb_functions.R @@ -106,7 +106,7 @@ guess_col_type <- function(tt) { types[names(col_dte[col_dte])] <- 3 # there are bools as well - col_log <- vapply(tt[!col_num], function(x) any(x == "b", na.rm = TRUE), NA) + col_log <- vapply(tt[!col_num], function(x) all(x == "b", na.rm = TRUE), NA) types[names(col_log[col_log])] <- 4 # or even hms diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp index e4723b330..3be586ef1 100644 --- a/src/helper_functions.cpp +++ b/src/helper_functions.cpp @@ -318,7 +318,7 @@ bool is_double(std::string x) { // [[Rcpp::export]] Rcpp::LogicalVector is_charnum(Rcpp::CharacterVector x) { Rcpp::LogicalVector out(x.size()); - for (size_t i = 0; i < x.size(); ++i) { + for (R_xlen_t i = 0; i < x.size(); ++i) { out[i] = is_double(Rcpp::as(x[i])); } return out; diff --git a/tests/testthat/test-read_from_created_wb.R b/tests/testthat/test-read_from_created_wb.R index 085303956..66a027b9f 100644 --- a/tests/testthat/test-read_from_created_wb.R +++ b/tests/testthat/test-read_from_created_wb.R @@ -187,3 +187,17 @@ test_that("wb_load contains path", { expect_equal(exp, got) }) + +test_that("column names are not missing with col_names = FALSE", { + dat <- data.frame( + numeric = 1:2, + character = c("a", "b"), + logical = c(TRUE, FALSE) + ) + + wb <- wb_workbook()$add_worksheet()$add_data(x = dat) + got <- as.character(wb_to_df(wb, col_names = FALSE)[2, ]) + exp <- c("1", "a", "TRUE") + expect_equal(exp, got) + +}) diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index 859b8a665..ba9adaa07 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -192,7 +192,7 @@ test_that("update cell(s)", { exp <- structure( list(c(7, NA, NA, NA, NA, 7), - c(NA, NA, TRUE, FALSE, TRUE, NA), + c("y", "z", "TRUE", "FALSE", "TRUE", NA), c(2, NA, 2.5, NA, NA, NA), c(7, 2, 3, NA, 5, NA)), names = c(NA, "x", "Var2", "Var3"),