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

[write] add cc$typ only with string_nums #1254

Merged
merged 1 commit into from
Jan 29, 2025
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
9 changes: 6 additions & 3 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,15 @@ write_data2 <- function(

rows_attr$r <- rownames(rtyp)


string_nums <- getOption("openxlsx2.string_nums", default = 0)

# original cc data frame
has_cm <- if (any(dc == openxlsx2_celltype[["cm_formula"]])) "c_cm" else NULL
has_typ <- if (string_nums) "typ" else NULL
nms <- c(
"r", "row_r", "c_r", "c_s", "c_t", has_cm,
"v", "f", "f_attr", "is", "typ"
"v", "f", "f_attr", "is", has_typ
)
cc <- create_char_dataframe(
colnames = nms,
Expand All @@ -443,8 +447,6 @@ write_data2 <- function(
}
}

string_nums <- getOption("openxlsx2.string_nums", default = 0)

na_missing <- FALSE
na_null <- FALSE

Expand Down Expand Up @@ -622,6 +624,7 @@ write_data2 <- function(
quotePrefix = "1",
numFmtId = "49"
)
cc$typ <- NULL
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/helper_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,12 @@ void wide_to_long(
int32_t in_string_nums = string_nums;

bool has_cm = zz.containsElementNamed("c_cm");
bool has_typ = zz.containsElementNamed("typ");

// pointer magic. even though these are extracted, they just point to the
// memory in the data frame
Rcpp::CharacterVector zz_c_cm;
Rcpp::CharacterVector zz_typ;

Rcpp::CharacterVector zz_row_r = Rcpp::as<Rcpp::CharacterVector>(zz["row_r"]);
if (has_cm) zz_c_cm = Rcpp::as<Rcpp::CharacterVector>(zz["c_cm"]);
Expand All @@ -411,7 +413,7 @@ void wide_to_long(
Rcpp::CharacterVector zz_is = Rcpp::as<Rcpp::CharacterVector>(zz["is"]);
Rcpp::CharacterVector zz_f = Rcpp::as<Rcpp::CharacterVector>(zz["f"]);
Rcpp::CharacterVector zz_f_attr = Rcpp::as<Rcpp::CharacterVector>(zz["f_attr"]);
Rcpp::CharacterVector zz_typ = Rcpp::as<Rcpp::CharacterVector>(zz["typ"]);
if (has_typ) zz_typ = Rcpp::as<Rcpp::CharacterVector>(zz["typ"]);
Rcpp::CharacterVector zz_r = Rcpp::as<Rcpp::CharacterVector>(zz["r"]);

// Convert na_strings only once outside the loop.
Expand Down Expand Up @@ -559,7 +561,7 @@ void wide_to_long(
}

// typ = std::to_string(vtyp)
SET_STRING_ELT(zz_typ, pos, Rf_mkChar(std::to_string(vtyp).c_str()));
if (has_typ) SET_STRING_ELT(zz_typ, pos, Rf_mkChar(std::to_string(vtyp).c_str()));

std::string cell_r = has_dims ? dims[static_cast<size_t>(idx)] : col + row;
SET_STRING_ELT(zz_r, pos, Rf_mkChar(cell_r.c_str()));
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-date_time_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test_that("convert hms works", {
exp <- data.frame(
r = "A1", row_r = "1", c_r = "A", c_s = "1", c_t = "",
v = "0.509189814814815",
f = "", f_attr = "", is = "", typ = "15",
f = "", f_attr = "", is = "",
stringsAsFactors = FALSE)
got <- wb$worksheets[[1]]$sheet_data$cc
expect_equal(exp, got)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-formulas.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ test_that("writing formulas with cell metadata works", {
r = "A1", row_r = "1", c_r = "A", c_s = "", c_t = "",
c_cm = "1", v = "", f = "SUM(ABS(A2:A11))",
f_attr = "t=\"array\" ref=\"A1\"", is = "",
typ = "14",
stringsAsFactors = FALSE)
got <- wb$worksheets[[1]]$sheet_data$cc[1, ]
expect_equal(exp, got)
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-save.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ test_that("write cells without data", {
v = c("", "", "", ""),
f = c("", "", "", ""),
f_attr = c("", "", "", ""),
is = c("", "", "", ""),
typ = c("3", "3", "3", "3")
is = c("", "", "", "")
),
row.names = c(NA, 4L),
class = "data.frame")
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-wb_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ test_that("creating a formula matrix works", {
shared = TRUE
)

exp <- c(210, 10)
exp <- c(210, 9)
got <- dim(wb$worksheets[[1]]$sheet_data$cc)
expect_equal(exp, got)

Expand All @@ -564,7 +564,7 @@ test_that("writing formula dataframes works", {
dims = wb_dims(x = df, from_row = 13, col_names = FALSE)
)

exp <- c(210, 10)
exp <- c(210, 9)
got <- dim(wb$worksheets[[1]]$sheet_data$cc)
expect_equal(exp, got)

Expand Down
26 changes: 3 additions & 23 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test_that("write_formula", {

cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E", ]
expect_equal(exp[1:10], got[1:10])
expect_equal(exp[1:9], got[1:9])


rownames(exp) <- 1L
Expand All @@ -36,7 +36,7 @@ test_that("write_formula", {

cc <- wb$worksheets[[1]]$sheet_data$cc
got <- cc[cc$row_r == "2" & cc$c_r == "E", ]
expect_equal(exp[1:10], got[1:10])
expect_equal(exp[1:9], got[1:9])

})

Expand Down Expand Up @@ -228,8 +228,7 @@ test_that("update cell(s)", {
v = c("", "", "", "", "", ""),
f = c("", "", "", "", "", ""),
f_attr = c("", "", "", "", "", ""),
is = c("", "", "", "", "", ""),
typ = c("4", "4", "4", "4", "4", "4")),
is = c("", "", "", "", "", "")),
row.names = 1:6,
class = "data.frame")
got <- head(wb$worksheets[[1]]$sheet_data$cc)
Expand Down Expand Up @@ -327,10 +326,6 @@ test_that("write character numerics with a correct cell style", {
got <- wb$styles_mgr$styles$cellXfs[2]
expect_equal(got, NA_character_)

exp <- c("4", "4", "4", "4", "4")
got <- wb$worksheets[[1]]$sheet_data$cc$typ
expect_equal(exp, got)

## string numerics correctly flagged
options("openxlsx2.string_nums" = 1)

Expand All @@ -351,18 +346,6 @@ test_that("write character numerics with a correct cell style", {
got <- wb$styles_mgr$styles$cellXfs[2]
expect_equal(exp, got)

exp <- c("4", "13", "4", "4", "13")
got <- wb$worksheets[[1]]$sheet_data$cc$typ
expect_equal(exp, got)

exp <- c("13", "2", "4")
got <- wb$worksheets[[2]]$sheet_data$cc$typ
expect_equal(exp, got)

exp <- c("2", "13", "2", "13")
got <- wb$worksheets[[3]]$sheet_data$cc$typ
expect_equal(exp, got)

## write string numerics as numerics (on the fly conversion)
options("openxlsx2.string_nums" = 2)

Expand All @@ -373,9 +356,6 @@ test_that("write character numerics with a correct cell style", {
got <- wb$styles_mgr$styles$cellXfs[2]
expect_equal(got, NA_character_)

exp <- c("4", "2", "4", "4", "2")
got <- wb$worksheets[[1]]$sheet_data$cc$typ
expect_equal(exp, got)
})

test_that("writing as shared string works", {
Expand Down
Loading