Skip to content

Undesired output #9

Description

@raffaelemancuso

Consider the following code:

gt2docx <- function(
  tbl,
  outfp,
  caption_text = "",
  caption_style = "Table Caption",
  caption_font_family = "Aptos",
  caption_font_size = 10,
  landscape = TRUE,
  fontname = "Aptos",
  fontsize = 10,
) {
  # Define Word section properties
  section_prop <- officer::prop_section(
    page_size = officer::page_size(
      orient = ifelse(landscape, "landscape", "portrait")
    )
  )

  # Add table caption
  tbl <- tbl %>%
      gt::tab_header(
      title = caption_text
    ) %>% 
    gt::tab_style(
      style = list(
        gt::cell_text(font = fontname, size = fontsize, align = align)
      ),
      locations = gt::cells_body()
    ) %>%
    gt::tab_style(
      style = list(
        gt::cell_text(
          font = caption_font_family,
          size = caption_font_size
        )
      ),
      locations = gt::cells_title()
    )

  # It's necessary to define the same default section than the one you want to end the document with so that Word agree to not add a page
  # See: https://stackoverflow.com/a/75451251/1719931
  officer::read_docx() %>%
    gto::body_add_gt(tbl, split=TRUE) %>%
    officer::body_end_block_section(
      value = officer::block_section(section_prop)
    ) %>%
    officer::body_set_default_section(section_prop) %>%
    print(target = outfp)
}
library(tidyverse)

varmean <- tribble(
  ~Dataset,
  ~Name)

for(i in seq(1, 50)) {
  varmean <- varmean %>%
    bind_rows(
      tibble(
        Dataset = paste0("Dataset ", i),
        Name = paste0("Variable ", i)
      )
    )
}

section_prop <- officer::prop_section(
  page_size = officer::page_size(
    orient = "landscape"
  ))

tbl <- varmean %>%
  gt::gt()  %>% 
  gt2docx("test.docx",
          caption_text = "Variable meanings and sources.",
          caption_font_size = 18,
          fontsize = 16,
          landscape = TRUE)

There are some things in the output I don't want:

  1. Word is telling me that there are fields refering toi external file when I open the resulting docx file. I don't want any field in the resulting file:
Image
  1. There is a "Table 1" before the title I don't want
Image
  1. The "1" in "Table 1" is in a different font size (and I think this is the field Word is referring to)

  2. The Table starts at page 2 instead of starting right after the title

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions