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

gt customisation options not persevered in .docx output #1098

Open
grt9 opened this issue Oct 31, 2022 · 5 comments
Open

gt customisation options not persevered in .docx output #1098

grt9 opened this issue Oct 31, 2022 · 5 comments

Comments

@grt9
Copy link

grt9 commented Oct 31, 2022

Hi!

I started using the .docx output format and love the way it works with multipage tables for a thesis/report. However, lots of the customisation options specified in my gt 'theme' are not preserved in the output. I'm trying to export gt tables with uniform formatting for inclusion in my PhD thesis

I tried digging around in the gt_save() script and also word export script but too complicated for me to deal with xml output

gt_theme <- function(data) {
  
  data %>% 
    tab_options(
      table_body.hlines.style = "hidden",
      table.border.top.style = "solid",
      table.border.top.color = "#000000",
      table.border.top.width = 3,
      
      table.border.bottom.style = "solid",
      table.border.bottom.color = "#000000",
      table.border.bottom.width = 3,
      
      column_labels.font.weight = "bold",
      column_labels.border.bottom.style = "solid",
      column_labels.border.bottom.color = "#000000",
      column_labels.border.bottom.width = 1.5,
      data_row.padding = 2
      
      
      
    )
  
} 

outputTable <-
DonorVariationTableAverages %>%
  dplyr::mutate(across(where(is.numeric), round, 2)) %>%
  gt() %>% 
  tab_spanner(
    label = "% of Live cells",
    columns = c(mean,
                SD,
                max,
                min,
                range)
  ) %>%
  
  sub_missing(columns = everything(),
              rows = everything(),
              missing_text = "-") %>%
  cols_align(
    align = "center",
    columns = everything()
  ) %>%
  cols_align(
    align = "left",
    columns = 1
  ) %>%
  cols_width(
    Population ~ px(150)
  ) %>% 
  
  gt_theme()
  

outputTable %>% 
  gtsave("outputTable.docx",
         path = processedExportDir)     
  

formatting looks like this in R:

image

but in MS Word now looks like this:

image

So it's losing

  • cell alignment
  • border styles + colours
  • adding cell borders

Many thanks!

@grt9
Copy link
Author

grt9 commented Oct 31, 2022

similar to #1089

@rich-iannone
Copy link
Member

Personal note: in talking with @thebioengineer , there might be an easier way to do this; basically just fold these options into the styles table (i.e., like using tab_style() internally). This aligns with recording which is these tab_options() options have been set by the user (i.e., non-default). We need to be careful not to overwrite specific styling set with tab_style()

@chase-eck
Copy link

Hi all,

I ran into this same issue and tried to switch to using tab_style() but the formatting options are not being preserved when outputting to .docx in an Rmarkdown notebook. I know you're planning on including this in v0.10.0, but was wondering if there was any interim solution/workaround for this.

@thebioengineer
Copy link
Collaborator

If you could make a reprex of what you are running, that would be great. I believe most tab_style options should be applied, it's the tab_options I haven't been able to put in yet. So knowing what I missed would be great.

@chase-eck
Copy link

Sure thing! An example is below using iris and gt v0.9.0 and R v4.2.3 Let me know if there's anything else that I can provide.

library(tidyverse)
library(gt)
data(iris)

iris %>% 
  mutate(has_vname = ifelse(Species != "setosa", "Has Vname", "No Vname")) %>%
  group_by(Species, has_vname) %>% 
  summarize(across(everything(), mean)) %>%
  ungroup() %>% 
  gt(groupname_col = "has_vname", 
     rowname_col = "Species") %>% 
  tab_spanner(label = "Variables", columns = everything()) %>% 
  tab_footnote("This is a footnote") %>% 
  # fill in top two rows and change font size to 15px
  tab_style(
    style = list(cell_fill(color = 'gray75'), 
                 cell_text('gray25', size = px(15))),
    locations = list(cells_column_labels(), cells_column_spanners(), cells_stubhead())
  ) %>%
  # Remove interior borders
  tab_style(
    style = cell_borders(
      sides = c("top", "bottom", "right"), 
      weight = NULL, 
      color = NULL
    ), 
    locations = list(cells_body(everything()), cells_column_spanners(), cells_row_groups(), cells_stub())
  ) %>% 
  # add bottom border for column labels and footnote
  tab_style(
    style = cell_borders(
      sides = c("bottom"), 
      weight = px(1.5),
      style = "solid", 
      color = "gray25"), 
    locations = list(cells_column_labels(), cells_footnotes())
  ) %>% 
  # change footnote font to 12px and make it gray
  tab_style(
    style = cell_text('gray25', size = px(12)), 
    locations = cells_footnotes()
  )
  


It renders the table below within the markdown:

image

But when I knit to a .docx it produces:

image

This is the table when I bump all of the fonts up to 12pt

image

So I'm having trouble with:

  • Rendering the border styles correctly in word (both removing interior ones and adding the bottom one to the footnote)
  • Taking out the white border between the tab_spanner and the col_labels (this is in both the R rendering and the .docx)
  • Setting fonts correctly -- it seems like whenever I set a font size it renders to 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment