Use package flextable to create tables with advanced formatting and layouts.

library(officer)
library(flextable)
library(magrittr)

Package flextable

Documentation can be found here: https://davidgohel.github.io/flextable/.

The flextable package has been written to enable production of tabular reporting; it is working very well with officer.

You can install it from CRAN:

install.packages("flextable")

The development version is available on Github:

devtools::install_github("davidgohel/flextable")

Example

Data used for illustrations is shown below:

data = structure(list(Status = c("Alive", "Alive", "Alive", "Alive","Melanoma", "Melanoma","Melanoma", "Melanoma", "Non-melanoma","Non-melanoma", "Non-melanoma", "Non-melanoma"), Gender = c("Female","Female", "Male", "Male", "Female", "Female", "Male", "Male","Female", "Female", "Male", "Male"), Ulceration = c("Absent","Present", "Absent", "Present", "Absent", "Present", "Absent","Present", "Absent", "Present", "Absent", "Present"), n = c(68L,23L, 24L, 19L, 8L, 20L, 8L, 21L, 3L, 4L, 4L, 3L), Mean = c(1.693,2.972, 1.468, 4.319, 2.139, 4.724, 3.266, 5.143, 1.667, 3.302,2.42, 8.053), SD = c(2.004, 2.593, 1.719, 2.423, 1.184, 4.128,4.681, 2.862, 1.141, 3.713, 2.499, 4.019)), class = "data.frame", .Names = c("Status","Gender", "Ulceration", "n", "Mean", "SD"), row.names = c(NA,-12L))
data
##          Status Gender Ulceration  n  Mean    SD
## 1         Alive Female     Absent 68 1.693 2.004
## 2         Alive Female    Present 23 2.972 2.593
## 3         Alive   Male     Absent 24 1.468 1.719
## 4         Alive   Male    Present 19 4.319 2.423
## 5      Melanoma Female     Absent  8 2.139 1.184
## 6      Melanoma Female    Present 20 4.724 4.128
## 7      Melanoma   Male     Absent  8 3.266 4.681
## 8      Melanoma   Male    Present 21 5.143 2.862
## 9  Non-melanoma Female     Absent  3 1.667 1.141
## 10 Non-melanoma Female    Present  4 3.302 3.713
## 11 Non-melanoma   Male     Absent  4 2.420 2.499
## 12 Non-melanoma   Male    Present  3 8.053 4.019

From these data, let’s create a simple flextable:

ft <- flextable(data = data) %>% 
  theme_booktabs() %>% 
  set_header_labels( n = "#", Mean = "\u03D1", SD = "\u03C3") %>% 
  color(i = ~ n < 4, color = "wheat") %>% 
  autofit() 

The table will be display as below:

Status

Gender

Ulceration

#

ϑ

σ

Alive

Female

Absent

68

1.693

2.004

Alive

Female

Present

23

2.972

2.593

Alive

Male

Absent

24

1.468

1.719

Alive

Male

Present

19

4.319

2.423

Melanoma

Female

Absent

8

2.139

1.184

Melanoma

Female

Present

20

4.724

4.128

Melanoma

Male

Absent

8

3.266

4.681

Melanoma

Male

Present

21

5.143

2.862

Non-melanoma

Female

Absent

3

1.667

1.141

Non-melanoma

Female

Present

4

3.302

3.713

Non-melanoma

Male

Absent

4

2.420

2.499

Non-melanoma

Male

Present

3

8.053

4.019

Add a flextable into a Word document

To insert that table in a Word document, function flextable::body_add_flextable is to be used.

read_docx() %>% 
  body_add_flextable(ft) %>% 
  print(target = "../assets/flextable/flextable.docx") %>% 
  invisible()

Download file flextable.docx - view with office web viewer

Add a flextable into a PowerPoint document

To insert that table in a PowerPoint document, functions flextable::ph_with_flextable() or flextable::ph_with_flextable_at() are to be used:

  • ph_with_flextable add the flextable into an available shape of the chosen layout
  • ph_with_flextable_at add the flextable at any arbitrary location in the current slide
read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_flextable(ft, type = "body") %>% 
  add_slide(layout = "Title Only", master = "Office Theme") %>% 
  ph_with_flextable_at(ft, left = 3, top = 4) %>% 
  print(target = "../assets/flextable/flextable.pptx") %>% 
  invisible()

Download file flextable.pptx - view with office web viewer