Skip to content

Word computed fields in a table #692

Closed

Description

@mikkmart

I'd like to include a paragraph containing a Word computed field inside a table. Is that possible somehow?

Some failed attempts:

library(officer)
library(flextable)
library(tibble)  # For sane creation of list columns.

page_par <- fpar("Page ", run_word_field("PAGE"))

read_docx() |> 
  body_add_fpar(page_par) |> 
  body_add_table(
    tibble(page_info = list(page_par))
  ) |> 
  body_add_flextable(
    tibble(page_info = list(page_par)) |> flextable()
  ) |>
  print(target = "foo.docx")
browseURL("foo.docx")
# unlink("foo.docx")

Activity

  1. davidgohel commented on Aug 12, 2025

    @davidgohel
    Owner

    Yes,

    This is the documentation https://davidgohel.github.io/flextable/reference/as_word_field.html

    and a copy of an example of this help page:

    library(officer)
    ft_2 <- flextable(head(cars))
    ft_2 <- add_footer_lines(ft_2, "temp text")
    ft_2 <- compose(
      x = ft_2, part = "footer", i = 1, j = 1,
      as_paragraph(
        "p. ",
        as_word_field(x = "Page", width = .05),
        " on ", as_word_field(x = "NumPages", width = .05)
      )
    )
    ft_2 <- autofit(ft_2, part = c("header", "body"))
    
    doc <- read_docx()
    doc <- body_add_flextable(doc, ft_2)
    doc <- body_add_break(doc)
    doc <- body_add_flextable(doc, ft_2)
    outfile <- print(doc, target = tempfile(fileext = ".docx"))
    
    
  2. Repository owner locked and limited conversation to collaborators on Aug 12, 2025
  3. converted this issue into a discussion #694 on Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions