Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wb_to_df] keep column names with col_names = FALSE for logical columns. closes #883 #884

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading