diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp index 0ad61ba4b..7770ce89d 100644 --- a/src/helper_functions.cpp +++ b/src/helper_functions.cpp @@ -514,7 +514,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(cvec[j]); std::string row = std::to_string(startrow); @@ -551,7 +551,7 @@ void wide_to_long( case character: // test if string can be written as number - if (string_nums && is_double(vals)) { + if (!(ColNames && (j == 0)) && string_nums && is_double(vals)) { cell.v = vals; if (string_nums == 1) { vtyp = string_num; diff --git a/tests/testthat/test-tables.R b/tests/testthat/test-tables.R index 07f57bc3e..75b244f8d 100644 --- a/tests/testthat/test-tables.R +++ b/tests/testthat/test-tables.R @@ -203,3 +203,23 @@ test_that("updating table works", { expect_equal(got, "A1:J4") }) + +test_that("writing table with factor titles works", { + df <- data.frame( + fct = structure( + 1:2, + levels = c( + "one", + "two" + ), + class = "factor"), + `1` = 1:2, + check.names = F + ) + + wb <- wb_workbook()$add_worksheet()$add_data_table(x = df) + + exp <- "1" + got <- wb$worksheets[[1]]$sheet_data$cc$is[[2]] + expect_equal(exp, got) +})