wdkrnls
wdkrnls

Reputation: 4692

flextable includes an R output instead of the table in Quarto render to docx

I have a block of code which generates a fairly complicated flextable.

In markdown the table gets its "live preview" inserted into the document as an overlay as expect. However, when I render to docx I see the following instead of a table:

a flextable object.
col_keys: `Reviewer`, `Book`, `Date`, `Index` 
header has 6 row(s) 
body has 1 row(s) 
original dataset sample: 
  Final Reviewer      Book Date Index
1    Mr. T 12-34-5678  2022-12-31                        BUS-I-NESS321
NULL

Why is that? How could I get the table to appear in the rendered document instead of this R gobbly-gook?

Here is how I generate my flextable:

library(dplyr)
library(tibble)
library(flextable)
author = "H. Smith"
title = "A plan for saving the day"
tibble(Reviewer = "Mr. T",
       Book = "12-34-5678", 
       Date = "2022-12-31", 
       Index = "BUS-I-NESS321") %>%
  flextable() %>%
  add_header_lines(paste(author, collapse = ", ")) %>%
  add_header_lines("Author(s)") %>%
  add_header_lines(title) %>%
  add_header_lines("Title") %>%
  add_header_lines("Super Official Report\nAcme Inc.") %>%
  theme_box() %>%
  bg(c(2, 4, 6), part = "header", bg = "grey80") %>%
  italic(c(2, 4, 6), part = "header") %>%
  align(1, align = "center", part = "header") %>%
  align(4, 4, align = "center", part = "header") %>% # doesn't work :()
  line_spacing(i = 1, space = 2, part = "header") %>%
  set_table_properties(layout = "autofit", width = .8) %>%
  print()

And here is a screenshot of how it looks:

fairly-complicated-flextable

Upvotes: 1

Views: 872

Answers (2)

Martin C. Arnold
Martin C. Arnold

Reputation: 9668

Using autofit() and flextable_to_rmd() usually gives decent results (may depend on the complexity of the flextable –– we cannot guess what 'fairly complicated' is) Here's an example from the docs.

Edit:

---
title: "Flextable Example"
format: docx
---

```{r, results='asis', echo=F, message = F}
library(dplyr)
library(tibble)
library(flextable)
author = "H. Smith"
title = "A plan for saving the day"
ft <- tibble(Reviewer = "Mr. T",
       Book = "12-34-5678", 
       Date = "2022-12-31", 
       Index = "BUS-I-NESS321") %>%
  flextable() %>%
  add_header_lines(paste(author, collapse = ", ")) %>%
  add_header_lines("Author(s)") %>%
  add_header_lines(title) %>%
  add_header_lines("Title") %>%
  add_header_lines("Super Official Report\nAcme Inc.") %>%
  theme_box() %>%
  bg(c(2, 4, 6), part = "header", bg = "grey80") %>%
  italic(c(2, 4, 6), part = "header") %>%
  align(1, align = "center", part = "header") %>%
  align(4, 4, align = "center", part = "header") %>% # doesn't work :()
  line_spacing(i = 1, space = 2, part = "header") %>%
  set_table_properties(layout = "autofit", width = .8)

ft <- autofit(ft)
flextable_to_rmd(ft)
```

enter image description here

Upvotes: 0

user2554330
user2554330

Reputation: 44868

The problem is that you are calling print(). In an R Markdown document, the print method produces the ugly output you saw. You need knitr::knit_print() (or nothing at all; autoprinting uses knit_print() there) to get the nicely formatted output. The same is true in Quarto, though it uses a hackish mechanism that doesn't work in a few weird cases.

Upvotes: 1

Related Questions