使用Package Officer和flextable显示多个表

时间:2019-12-18 20:59:35

标签: r function flextable officer reporters

我想知道是否可以使用flextableflextable以HTML格式(在单个页面上)显示多个officer

 library('flextable')
 library('officer')

dat1 <- data.frame(Approaches = c("Y", "Y", "N"), Meets = c("N", "Y", "N"), row.names = c("Read", "Math", "Sci."))
dat2 <- data.frame(Read = "Y", Math = "N")

def_par <- fp_par(text.align = "center")
def_txt <- fp_text(bold = TRUE)


ft <- flextable(dat1, cwidth = c(3.2, 3.2))      # Table #1

ft <- style(ft, pr_p = def_par, part = "all")
ft <- style(ft, pr_t = def_txt, part = "header")

tit <- c("Domain 1 and Domain 2B", "Domain 2A")

ft <- set_caption(ft, tit[1])

ft <- add_footer_lines(ft, values = "") # add a line break

flextable(dat2, cwidth = c(3.2, 3.2))            # Table #2

1 个答案:

答案 0 :(得分:2)

在rmarkdown文档中可以轻松完成

---
title: "test"
author: "akrun"
date: "12/18/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library('officer')
library(flextable)

```


```{r echo=FALSE, results='asis'}
library(htmltools)
dat1 <- data.frame(Approaches = c("Y", "Y", "N"), Meets = c("N", "Y", "N"), row.names = c("Read", "Math", "Sci."))
dat2 <- data.frame(Read = "Y", Math = "N")

def_par <- fp_par(text.align = "center")
def_txt <- fp_text(bold = TRUE)


ft <- flextable(dat1, cwidth = c(3.2, 3.2))      # Table #1

ft <- style(ft, pr_p = def_par, part = "all")
ft <- style(ft, pr_t = def_txt, part = "header")

tit <- c("Domain 1 and Domain 2B", "Domain 2A")

ft <- set_caption(ft, tit[1])

ft <- add_footer_lines(ft, values = "") # 
ft

```


```{r echo=FALSE, results='asis'}
ft2 <- flextable(dat2, cwidth = c(3.2, 3.2)) 
ft2 <- style(ft2, pr_p = def_par, part = "all")
ft2 <- style(ft2, pr_t = def_txt, part = "header")

tit <- c("Read", "Math")
ft2 <- set_caption(ft2, tit[1])
ft2 <- add_footer_lines(ft2, values = "") # 
ft2
```

-输出

enter image description here


R studio中,首先创建一个markdown文档(文件->新文件-> R markdown ..),

enter image description here

将上述代码粘贴到文档中,将其保存在文件/文件夹中,然后单击knit enter image description here