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] table column names have to be characters #1071

Merged
merged 1 commit into from
Jul 2, 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
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;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, we never reset string_nums when a vtype factor was present.

// 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)
})
Loading