Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Formatting is different if title and footer is added #29

Closed
RobinL opened this issue Aug 18, 2017 · 1 comment
Closed

Formatting is different if title and footer is added #29

RobinL opened this issue Aug 18, 2017 · 1 comment
Assignees

Comments

@RobinL
Copy link
Member

RobinL commented Aug 18, 2017

Compare the results of the following two chunks:
xltabr:::combine_all_styles(tab) gives the same result for all the body cells, but the styling that's actually applied to the workbook is different

# Test 1
ct <- reshape2::dcast(mtcars, am + gear ~ cyl, value.var= "mpg", margins=c("am", "gear"), fun.aggregate = mean)
headers <- colnames(ct)
tab <- xltabr::initialise()
tab <- xltabr::add_top_headers(tab, headers)
tab <- xltabr::add_body(tab, ct)
tab <- xltabr:::auto_detect_left_headers(tab)
tab <- xltabr:::auto_detect_body_title_level(tab)
tab <- xltabr:::auto_style_indent(tab)
tab <- xltabr::auto_style_number_formatting(tab)
tab <- xltabr:::write_all_elements_to_wb(tab)
tab <- xltabr:::add_styles_to_wb(tab)
xltabr:::combine_all_styles(tab)

if (open_output) openxlsx::openXL(tab$wb) else {
  if (file.exists("test1.xlsx")) file.remove("test1.xlsx")
  openxlsx::saveWorkbook(tab$wb, "test1.xlsx")
}

tab$body$body_df
# Test 2
ct <- reshape2::dcast(mtcars, am + gear ~ cyl, value.var= "mpg", margins=c("am", "gear"), fun.aggregate = mean)
headers <- colnames(ct)
tab <- xltabr::initialise()
tab <- xltabr::add_top_headers(tab, headers)
tab <- xltabr::add_body(tab, ct)
tab <- xltabr::add_title(tab, "Here is a title")
tab <- xltabr::add_footer(tab, "Here is a footer")
tab <- xltabr:::auto_detect_left_headers(tab)
tab <- xltabr:::auto_detect_body_title_level(tab)
tab <- xltabr:::auto_style_indent(tab)
tab <- xltabr::auto_style_number_formatting(tab)
tab <- xltabr:::write_all_elements_to_wb(tab)
tab <- xltabr:::add_styles_to_wb(tab)
xltabr:::combine_all_styles(tab)

if (open_output) openxlsx::openXL(tab$wb) else {
  if (file.exists("test2.xlsx")) file.remove("test2.xlsx")
  openxlsx::saveWorkbook(tab$wb, "test2.xlsx")
}
@isichei
Copy link
Contributor

isichei commented Aug 23, 2017

Fixed. Problem occurred as string columns returned from ####_get_cell_styles_table are factors. I Passing these factors into the cell_style_catalogue as a key accessors was messing things up - we didn't see this for test1 because we were only using one table from body_get_cell_style_table. In test2 we combine body_get_cell_style_table and title_get_cell_style_table in doing so I think the factors get messed up.

Anyway I've fixed the issue by
full_table <- data.frame(lapply(full_table, as.character), stringsAsFactors=FALSE)

However, I think we need to change the outputs from ####_get_cell_style_tables to data.frames without factors as this may cause more issues further down the line.

Will add a new issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants