Skip to content

Commit

Permalink
[write] table column names have to be characters (#1071)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Jul 2, 2024
1 parent 7c94ede commit e639220
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/helper_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ void wide_to_long(

auto startcol = start_col;

int32_t in_string_nums = string_nums;

// pointer magic. even though these are extracted, they just point to the
// memory in the data frame
Rcpp::CharacterVector zz_row_r = Rcpp::as<Rcpp::CharacterVector>(zz["row_r"]);
Expand Down Expand Up @@ -514,7 +516,7 @@ void wide_to_long(

int8_t vtyp = (int8_t)vtyps[i];
// if colname is provided, the first row is always a character
if (ColNames & (j == 0)) vtyp = character;
if (ColNames && j == 0) vtyp = character;
std::string vals = Rcpp::as<std::string>(cvec[j]);
std::string row = std::to_string(startrow);

Expand All @@ -524,8 +526,13 @@ void wide_to_long(
if (ref_str.compare("0") == 0)
ref_str = col + row;

// factors can be numeric or string or both
if (vtyp == factor) string_nums = true;
// factors can be numeric or string or both. tables require the
// column name to be character and once we have overwritten for
// a factor, we have to reset string_nums.
if (!(ColNames && j == 0) && vtyp == factor)
string_nums = 1;
else
string_nums = in_string_nums;

// create struct
celltyp cell;
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,23 @@ test_that("updating table works", {
expect_equal(got, "A1:J4")

})

test_that("writing table headers with factor variables works", {
df <- data.frame(
fct = structure(
1:2,
levels = c(
"one",
"two"
),
class = "factor"),
`1` = 1:2,
check.names = FALSE
)

wb <- wb_workbook()$add_worksheet()$add_data_table(x = df)

exp <- "<is><t>1</t></is>"
got <- wb$worksheets[[1]]$sheet_data$cc$is[[2]]
expect_equal(exp, got)
})

0 comments on commit e639220

Please sign in to comment.