Skip to content

Commit

Permalink
[wb_to_df] keep column names with col_names = FALSE for logical col…
Browse files Browse the repository at this point in the history
…umns. closes #883 (#884)

* [wb_to_df] don't drop column names for logical columns with `col_names = FALSE`. closes #883

* update NEWS

* fix compiler warning
  • Loading branch information
JanMarvin committed Jan 4, 2024
1 parent f5d62ac commit 91dccad
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Expand Up @@ -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)


***************************************************************************

Expand Down
2 changes: 1 addition & 1 deletion R/wb_functions.R
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/helper_functions.cpp
Expand Up @@ -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<std::string>(x[i]));
}
return out;
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test-read_from_created_wb.R
Expand Up @@ -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)

})
2 changes: 1 addition & 1 deletion tests/testthat/test-write.R
Expand Up @@ -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"),
Expand Down

0 comments on commit 91dccad

Please sign in to comment.