Displaying multiple tables using Package officer and flextable

非 Y 不嫁゛ 提交于 2019-12-23 01:24:09

问题


I was wondering if it might be possible to display more than one flextable in the HTML format (on a single page) using flextable and 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:


It can be done easily in a rmarkdown document

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

-output


In R studio, first create a markdown document (File -> New File -> R markdown ..),

Paste the above code in the document, save it in a file/folder and then click on knit



来源:https://stackoverflow.com/questions/59399901/displaying-multiple-tables-using-package-officer-and-flextable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!