简体   繁体   English

flextable 包含一个 R 输出,而不是 Quarto 中的表格呈现为 docx

[英]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 .我有一段代码生成一个相当复杂的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:但是,当我呈现为 docx 时,我看到的是以下内容而不是表格:

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?我怎样才能让表格出现在呈现的文档中,而不是这个 R gobbly-gook?

Here is how I generate my flextable :以下是我生成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:这是它的外观截图:

相当复杂灵活

The problem is that you are calling print() .问题是您正在调用print() In an R Markdown document, the print method produces the ugly output you saw.在 R Markdown 文档中,print 方法会产生您看到的难看的输出。 You need knitr::knit_print() (or nothing at all; autoprinting uses knit_print() there) to get the nicely formatted output.您需要knitr::knit_print() (或根本不需要;自动打印在那里使用knit_print() )来获得格式良好的输出。 The same is true in Quarto, though it uses a hackish mechanism that doesn't work in a few weird cases.在 Quarto 中也是如此,尽管它使用了一种在一些奇怪的情况下不起作用的骇人听闻的机制。

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.使用 autofit( autofit()flextable_to_rmd()通常会给出不错的结果(可能取决于 flextable 的复杂性——我们无法猜测“相当复杂”是什么)这是来自文档的示例。

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)
```

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM