moj-analytical-services / xltabr

xltabr: An R package for writing formatted cross tabulations (contingency tables) to Excel using openxlsx
https://moj-analytical-services.github.io/xltabr/
32 stars 6 forks source link

Fails when style[["numFmt"]] is null #19

Closed RobinL closed 7 years ago

RobinL commented 7 years ago

Tracked bug down to here: https://github.com/moj-analytical-services/xltabr/blob/dc005be14e92a0a1715a76da45c010b5bc0c8b69/R/style_catalogue.R#L202

Whehn openxlsx::createStyle get passed numFmt =NULL it raises and error Error in if (numFmt == "date") { : argument is of length zero (the error seems to be unrelated to "date", it's just that when numFmt is NULL the if statement cannot evaluate.

To reproduce this error, use the following code:

library(magrittr)

headers <- names(mtcars)

tab <- xltabr::initialise() %>%
  xltabr::add_top_headers(headers) %>%
  xltabr::add_body(mtcars) %>% 
  xltabr::auto_style_number_formatting()

tab <- xltabr:::write_all_elements_to_wb(tab)

xltabr:::add_styles_to_wb(tab)

Note output from

xltabr:::top_headers_get_cell_styles_table(tab)
xltabr:::body_get_cell_styles_table(tab)

looks fine

isichei commented 7 years ago

Fixed - when converting a style list to a S4 style object if the numFmt is NULL the code will pass "GENERAL" into openxlsx::createStyle for numFmt (which is the default)