在rmarkdown中使用gtsummary和flextable生成表格。

问题描述 投票:1回答:1

大多数合作者都喜欢用word格式的表格。随着rmarkdown,knitr,gtsummary和flextable的出现,这个时代终于到来了,但我无法理解如何在不手动设置缩进的情况下生成下面的最终表格。我认为下面的表一在行间留出了太多的空气,但我想不出如何在程序上将行距设置得更紧(试过autofit、height、height_all、hrule都没有得到理想的输出)。相反,我使用word中的紧凑样式来生成下面的tbl 2。但是,我必须手动插入圆柱类别的缩进。有谁知道如何通过编程来实现?


title: "testing T´s"
output:
  word_document:
    reference_docx: temp.docx
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: inline
---

Plain 
====
```{r results='asis',echo=FALSE,message=FALSE}
library(gtsummary)
library(flextable)
set_gtsummary_theme(theme_gtsummary_jama())
a <- mtcars[1:20,c(1,2,9,4)] 
  b <- tbl_summary(a,
    missing="ifany",
    by=am,
    type=list(cyl~"categorical"))%>% 
      bold_labels() %>%
  add_p() %>% add_overall()
```


Flextable
====
```{r results='asis',echo=FALSE,message=FALSE}
fl <- gtsummary::as_flextable(b) %>%  font(fontname = "Bodoni 72",part = "all") %>% fontsize(size=8,part="all") %>%  autofit(add_h = -.5) 
fl
```

enter image description here

r-markdown word flextable gtsummary
1个回答
3
投票

目前,还没有一个简单的方法可以做到这一点。但我已经包含了一个代码示例,我认为它确实可以解决你的问题。

对于{flextable}来说,调用函数的顺序很重要。运行 as_flextable() 然后添加额外的调用似乎并不能得到你想要的东西。

另一种方法是保存调用,在需要的地方插入新的flextable函数调用,然后评估这些调用。下面的例子就是这样做的。

---
title: "Untitled"
output: word_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
```


```{r}
library(tidyverse)
library(gtsummary)
library(flextable)

set_gtsummary_theme(theme_gtsummary_jama())

tbl <- 
  mtcars[1:20, c(1, 2, 9, 4)] %>%
  tbl_summary(
    missing = "ifany",
    by = am,
    type = list(cyl ~ "categorical")
  ) %>%
  bold_labels() %>%
  add_p() %>%
  add_overall()
```


### Default Flextable

```{r}
gtsummary::as_flextable(tbl)
```

### Compact Flextable

```{r}
# this function inserts additional flextable calls, then evaluates the calls
update_flextable_calls <- function(x, call_list, after) {
  # saving calls that create the flextable
  x_calls <- gtsummary::as_flextable(x, return_calls = TRUE)

  # adding new calls at `after=`
  after_n <- names(x_calls) %in% after %>% which()
  x_calls <- c(
    x_calls[1:after_n],
    call_list,
    x_calls[(after_n + 1):length(x_calls)]
  )

  # evaluating calls
  x_calls %>%
    unlist() %>%
    purrr::compact() %>%
    # concatenating expressions with %>% between each of them
    purrr::reduce(function(x, y) rlang::expr(!!x %>% !!y)) %>%
    # evaluating expressions
    eval()
}

# list of calls that make a table compact
compact_calls <- list(
  rlang::expr(font(fontname = "Bodoni 72", part = "all")),
  rlang::expr(fontsize(size = 8, part = "all")),
  rlang::expr(padding(padding.top = 0, part = "all")),
  rlang::expr(padding(padding.bottom = 0, part = "all"))
)

# adding the compact calls, and evaluating them
update_flextable_calls(
  x = tbl, # gtsummary table
  call_list = compact_calls, # calls that make flextable compact
  after = "footnote" # add calls after the "footnote" functions
) 
```

enter image description here

这显然不是一个很好的永久性解决方案。 我们有一个主题叫做 theme_gtsummary_compact() 使得{gt}表的字体更小,间距更小。我们可以更新主题,让 flextables 也变得更紧凑。我希望你能在GitHub上创建一个问题,以更新 theme_gtsummary_compact() 对于可弯曲的,我们可以合作制定一个适合您的解决方案。https:/github.comddsjoberggtsummaryissuesnewchoose。

© www.soinside.com 2019 - 2024. All rights reserved.