I am using huxtable in Quarto in R for a pretty complicated table, and I need to make sure I have running headers when I knit to word. The headers will have their own formatting that needs to be maintained, as well as the formatting throughout the rest of the table.
In the example below I am using as some trial-and-error data, I am able to break the huxtable into multiple parts using split_across which then has a running header that consists of 3 rows. In R, it renders with the text color and bold. But when I Render the qmd file to Word, it removes the special formatting. Has anyone else had this problem? If so, is there a work around, or am I missing something in my code? Or is there a better alternative out there? Any suggestions much appreciated!
---
format: docx
editor: visual
---
{r test, echo=FALSE, message=FALSE, warning=FALSE}
require(huxtable)
myfaketable <- data.frame(yee = rep("test1", 50),
haw = rep("test2", 50))
myfaketable[1,] <- c("this is", "header 1")
myfaketable[2,] <- c("this is", "header 2")
myfaketable2 <- huxtable::as_hux(myfaketable) |>
huxtable::set_all_borders(brdr(1, "solid", "black"))
myfaketable2 <- huxtable::set_header_rows(myfaketable2, 1, TRUE)
myfaketable2 <- huxtable::set_header_rows(myfaketable2, 2, TRUE)
myfaketable2 <- huxtable::set_header_rows(myfaketable2, 3, TRUE)
myfaketable2 <- style_headers(myfaketable2,
bold = TRUE,
text_color = "purple"
)
myfaketable2_list <- (split_across(myfaketable2, after = c(15, 30, 45), headers = TRUE))
myfaketable2_list
I have knit the document both with and without a custom reference doc (will be using a custom reference doc for the actual data), but neither keeps the formatting. I tried converting the list to a flextable to see if I could make something work there, but I can't convert a list (and I'm not sure it would maintain my other formatting anyways).
I figured this out with help from an answer to another question involving kable: https://github.com/yihui/knitr/issues/886
The exact solution did not work, but I was able to get each part of the huxtable to print by looping over the length of the huxtable list and using knit_print
. I also had to use cat()
- without it, the table did not render in the word document. This maintained the formatting in the Word document from Quarto.
for(i in 1:length(myfaketable2_list)){
cat('\n testin')
cat(knitr::knit_print(huxtable:::as_hux(myfaketable2_list[[i]])))
cat('\n testout')
}