08-1: Making Tables with flextable

Tips to make the most of the lecture notes

  • Click on the three horizontally stacked lines at the bottom left corner of the slide, then you will see table of contents, and you can jump to the section you want

  • Hit letter β€œo” on your keyboard and you will have a panel view of all the slides

  • The box area with a hint of blue as the background color is where you can write code (hereafter referred to as the β€œcode area”).
  • Hit the β€œRun Code” button to execute all the code inside the code area.
  • You can evaluate (run) code selectively by highlighting the parts you want to run and hitting Command + Enter for Mac (Ctrl + Enter for Windows).
  • If you want to run the codes on your computer, you can first click on the icon with two sheets of paper stacked on top of each other (top right corner of the code chunk), which copies the code in the code area. You can then paste it onto your computer.
  • You can click on the reload button (top right corner of the code chunk, left to the copy button) to revert back to the original code.

Create tables with the flextable package


Taste of the flextable package

head(mtcars, 20)
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat)
                    am carb gear  mpg drat
Mazda RX4            1    4    4 21.0 3.90
Mazda RX4 Wag        1    4    4 21.0 3.90
Datsun 710           1    1    4 22.8 3.85
Hornet 4 Drive       0    1    3 21.4 3.08
Hornet Sportabout    0    2    3 18.7 3.15
Valiant              0    1    3 18.1 2.76
Duster 360           0    4    3 14.3 3.21
Merc 240D            0    2    4 24.4 3.69
Merc 230             0    2    4 22.8 3.92
Merc 280             0    4    4 19.2 3.92
Merc 280C            0    4    4 17.8 3.92
Merc 450SE           0    3    3 16.4 3.07
Merc 450SL           0    3    3 17.3 3.07
Merc 450SLC          0    3    3 15.2 3.07
Cadillac Fleetwood   0    4    3 10.4 2.93
Lincoln Continental  0    4    3 10.4 3.00
Chrysler Imperial    0    4    3 14.7 3.23
Fiat 128             1    1    4 32.4 4.08
Honda Civic          1    2    4 30.4 4.93
Toyota Corolla       1    1    4 33.9 4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable()

am

carb

gear

mpg

drat

1

4

4

21.0

3.90

1

4

4

21.0

3.90

1

1

4

22.8

3.85

0

1

3

21.4

3.08

0

2

3

18.7

3.15

0

1

3

18.1

2.76

0

4

3

14.3

3.21

0

2

4

24.4

3.69

0

2

4

22.8

3.92

0

4

4

19.2

3.92

0

4

4

17.8

3.92

0

3

3

16.4

3.07

0

3

3

17.3

3.07

0

3

3

15.2

3.07

0

4

3

10.4

2.93

0

4

3

10.4

3.00

0

4

3

14.7

3.23

1

1

4

32.4

4.08

1

2

4

30.4

4.93

1

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla()

am

carb

gear

mpg

drat

1

4

4

21.0

3.90

1

4

4

21.0

3.90

1

1

4

22.8

3.85

0

1

3

21.4

3.08

0

2

3

18.7

3.15

0

1

3

18.1

2.76

0

4

3

14.3

3.21

0

2

4

24.4

3.69

0

2

4

22.8

3.92

0

4

4

19.2

3.92

0

4

4

17.8

3.92

0

3

3

16.4

3.07

0

3

3

17.3

3.07

0

3

3

15.2

3.07

0

4

3

10.4

2.93

0

4

3

10.4

3.00

0

4

3

14.7

3.23

1

1

4

32.4

4.08

1

2

4

30.4

4.93

1

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon")

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

1

4

4

21.0

3.90

1

1

4

22.8

3.85

0

1

3

21.4

3.08

0

2

3

18.7

3.15

0

1

3

18.1

2.76

0

4

3

14.3

3.21

0

2

4

24.4

3.69

0

2

4

22.8

3.92

0

4

4

19.2

3.92

0

4

4

17.8

3.92

0

3

3

16.4

3.07

0

3

3

17.3

3.07

0

3

3

15.2

3.07

0

4

3

10.4

2.93

0

4

3

10.4

3.00

0

4

3

14.7

3.23

1

1

4

32.4

4.08

1

2

4

30.4

4.93

1

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit()

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

1

4

4

21.0

3.90

1

1

4

22.8

3.85

0

1

3

21.4

3.08

0

2

3

18.7

3.15

0

1

3

18.1

2.76

0

4

3

14.3

3.21

0

2

4

24.4

3.69

0

2

4

22.8

3.92

0

4

4

19.2

3.92

0

4

4

17.8

3.92

0

3

3

16.4

3.07

0

3

3

17.3

3.07

0

3

3

15.2

3.07

0

4

3

10.4

2.93

0

4

3

10.4

3.00

0

4

3

14.7

3.23

1

1

4

32.4

4.08

1

2

4

30.4

4.93

1

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb"))

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header")

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header")

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1) %>%
  fontsize(i = 12:18, size = 8)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1) %>%
  fontsize(i = 12:18, size = 8) %>%
  add_footer_row(values = "blah blah", colwidths = 5)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

blah blah

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1) %>%
  fontsize(i = 12:18, size = 8) %>%
  add_footer_row(values = "blah blah", colwidths = 5) %>%
  border_outer(fp_border(color="red", width = 2))

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

blah blah

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1) %>%
  fontsize(i = 12:18, size = 8) %>%
  add_footer_row(values = "blah blah", colwidths = 5) %>%
  border_outer(fp_border(color="red", width = 2)) %>%
  line_spacing(space = 1.5)

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

blah blah

Taste of the flextable package

head(mtcars, 20) %>%
  select(am, carb, gear, mpg, drat) %>%
  flextable() %>%
  theme_vanilla() %>%
  set_header_labels(mpg = "miles per gallon") %>%
  autofit() %>%
  merge_v(j = c("am", "carb")) %>%
  italic(j = 1) %>%
  bg(bg = "#C90000", part = "header") %>%
  color(color = "blue", j = 5) %>%
  color(color = "red", i = 5:10, j = 2) %>%
  color(color = "white", part = "header") %>%
  bold(~ drat > 3.2, ~ gear, bold = TRUE) %>%
  align(j = 1) %>%
  fontsize(i = 12:18, size = 8) %>%
  add_footer_row(values = "blah blah", colwidths = 5) %>%
  border_outer(fp_border(color="red", width = 2)) %>%
  line_spacing(space = 1.5) %>%
  theme_tron()

am

carb

gear

miles per gallon

drat

1

4

4

21.0

3.90

4

21.0

3.90

1

4

22.8

3.85

0

3

21.4

3.08

2

3

18.7

3.15

1

3

18.1

2.76

4

3

14.3

3.21

2

4

24.4

3.69

4

22.8

3.92

4

4

19.2

3.92

4

17.8

3.92

3

3

16.4

3.07

3

17.3

3.07

3

15.2

3.07

4

3

10.4

2.93

3

10.4

3.00

3

14.7

3.23

1

1

4

32.4

4.08

2

4

30.4

4.93

1

4

33.9

4.22

blah blah

Create table and modify Parts

Install the following packages and library them.

#--- install if you have not ---#
install.packages("flextable")
install.packages("officer")

#--- library ---#
library(flextable)
library(officer)
#--- Define regions ---#
Australasia <- c("AU", "NZ")
Melanesia <- c("NC", "PG", "SB", "VU")
Polynesia <- c("PF", "WS", "TO", "TV")

library(gt)

#--- create a dataset ---#
(
tab_data <- 
  countrypops %>%
  filter(country_code_2 %in% c(
    Australasia, Melanesia, Polynesia
  )) %>%
  filter(year %in% c(1995, 2005, 2015)) %>%
  mutate(region = case_when(
    country_code_2 %in% Australasia ~ "Australasia",
    country_code_2 %in% Melanesia ~ "Melanesia",
    country_code_2 %in% Polynesia ~ "Polynesia",
  )) %>%
  pivot_wider(
    values_from = population,
    names_from = year,
    names_prefix = "y_"
  ) %>%
  arrange(region, desc(y_2015)) %>%
  select(-starts_with("country_code")) %>%
  mutate(
    pop_ratio_10_15 = y_2015 / y_2005,
    date = "2013-11-14"
  )
)
# A tibble: 10 Γ— 7
   country_name     region        y_1995   y_2005   y_2015 pop_ratio_10_15 date 
   <chr>            <chr>          <int>    <int>    <int>           <dbl> <chr>
 1 Australia        Australasia 18004882 20176844 23815995            1.18 2013…
 2 New Zealand      Australasia  3673400  4133900  4609400            1.12 2013…
 3 Papua New Guinea Melanesia    4616439  6498818  8682174            1.34 2013…
 4 Solomon Islands  Melanesia     375189   482486   612660            1.27 2013…
 5 Vanuatu          Melanesia     170612   217632   276438            1.27 2013…
 6 New Caledonia    Melanesia     193816   232250   269460            1.16 2013…
 7 French Polynesia Polynesia     231446   271060   291787            1.08 2013…
 8 Samoa            Polynesia     174902   188626   203571            1.08 2013…
 9 Tonga            Polynesia      99977   105633   106122            1.00 2013…
10 Tuvalu           Polynesia       9585     9912    10877            1.10 2013…

We can apply flextable() to a data.frame to initiate a table:

flextable(
  tab_data, 
  col_keys = 
    c(
      "country_name",
      "lufhierh",
      "region",
      "y_1995",
      "y_2005"
    )
 )

where col_keys are the list of the name of the variables from the data.frame (providing variable names that do no exist in the dataset creates blank columns)

flextable(
  tab_data,
  col_keys = 
    c(
      "country_name", 
      "lufhierh", 
      "region", 
      "y_1995", 
      "y_2005"
    )
)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Selector

Many functions let you choose specifically where you apply changes. Those functions have

  • i for selecting rows
    • formula
    • integer vector
    • logical vector
  • j for selecting columns
    • formula
    • character vector
    • integer vector

You can use any combinations of the reference methods for i and j.

We will be building on ft created below:

ft <-
  tab_data %>% 
  flextable(
    col_keys = 
      c(
        "country_name",
        "region",
        "y_1995",
        "y_2005"
      )
  ) 

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Syntax

function(
  i = ~ condition based on variables,
  j = ~ vairable 1 + variable 2 + ...,
  .
)


Example

ft %>%
  color(
    i = ~ y_2005 > 2e5,
    j = ~ country_name + region,
    color = "red"
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Using a character vector for j is not recommended because using a formula involves less typing.

ft %>%
  color(
    i = c(2, 4:9),
    j = c("country_name", "y_1995"),
    color = "red"
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

ft %>% 
  color(
    i = rep(c(TRUE, FALSE), each = 5),
    j = c(1, 3),
    color = "red"
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

You can refer to parts of the table using part = option. The available options are

  • header: the header part of the table
  • footer: the footer part of the table
  • body: the body part of the table
  • all: the body and the header parts of the table


tab_data %>% 
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>% 
  color(
    part = "header",
    color = "red"
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Different functions have different default values for part.

Style

You can use the style() function to change the style and format of a table.

Syntax

style(
  x,
  i = NULL,
  j = NULL,
  pr_t = NULL,
  pr_p = NULL,
  pr_c = NULL,
  part = "body"
)

We can use fp_*() functions from the officer package to specify the style of texts, paragraphs, and cells.

  • pr_t = fp_text(): format texts
  • pr_p = fp_par(): format paragraphs
  • pr_c = fp_celll(): format cells

(pr in pr_* = stands for property.)

fp_text() lets you update the appearance of texts, including color, font size, bold or not, etc (see the help page below for the complete list of options).


Syntax

ft %>% 
  style(
    i = 4:6,
    j = ~ country_name,
    pr_t = 
      fp_text(
        color = "red",
        bold = TRUE,
        underlined = TRUE
      )
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

  • paragraphs: rectangular boxes around the texts inside the cells
  • cells: rectangular boxes that contain paragraphs and texts inside
ft %>% 
  style(
    pr_p = fp_par(
      border.bottom = fp_border(width = 2, color = "blue")
    ),
    pr_c = fp_cell(
      border.bottom = fp_border(width = 2, color = "red")
    )
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

fp_paragraph() lets you update the appearance of paragraphs (see the help page below for the complete list of options).


Syntax

ft %>% 
  style(
    i = 4:6,
    j = ~ country_name,
    pr_p = fp_par(
      text.align = "left",
      shading.color = "grey"
    )
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

fp_cell() lets you update the appearance of cells (see the help page below for the complete list of options).


Syntax

?fp_cell
ft %>% 
  style(
    i = 4:6,
    j = ~ country_name,
    pr_c = fp_cell(
      margin = 2,
      vertical.align = "top",
      background.color = "red"
    )
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Formatting: convenience functions

  • Instead of using the style() function in combination with fp_*() functions, you can easily work on a specific aesthetic feature using convenience functions like below:
    • align(), align_text_col(), align_nottext_col(): Set text alignment
    • bg(): Set background color
    • font(): Set font
    • fontsize(): Set font size
    • italic(): Set italic font
    • bold(): Set bold font
    • color(): Set font color
    • padding(): Set paragraph paddings
    • valign(): Set vertical alignment
    • rotate(): rotate cell text
    • empty_blanks(): make blank columns as transparent
  • You can use the selector syntax to specify where the specified effects take place
ft %>%
  color(
    i = 1,
    j = 2,
    "#fcba03"
  ) %>%
  bg(
    i = ~ y_2005 < 2e5,
    j = c("region"),
    bg = "grey"
  ) %>%
  fontsize(
    i = 7,
    size = 16
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data
# A tibble: 10 Γ— 7
   country_name     region        y_1995   y_2005   y_2015 pop_ratio_10_15 date 
   <chr>            <chr>          <int>    <int>    <int>           <dbl> <chr>
 1 Australia        Australasia 18004882 20176844 23815995            1.18 2013…
 2 New Zealand      Australasia  3673400  4133900  4609400            1.12 2013…
 3 Papua New Guinea Melanesia    4616439  6498818  8682174            1.34 2013…
 4 Solomon Islands  Melanesia     375189   482486   612660            1.27 2013…
 5 Vanuatu          Melanesia     170612   217632   276438            1.27 2013…
 6 New Caledonia    Melanesia     193816   232250   269460            1.16 2013…
 7 French Polynesia Polynesia     231446   271060   291787            1.08 2013…
 8 Samoa            Polynesia     174902   188626   203571            1.08 2013…
 9 Tonga            Polynesia      99977   105633   106122            1.00 2013…
10 Tuvalu           Polynesia       9585     9912    10877            1.10 2013…

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03")

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey")

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times")

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times") %>%
#--- font size ---#
  fontsize(i = 7, size = 16)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times") %>%
#--- font size ---#
  fontsize(i = 7, size = 16) %>%
#--- italicize ---#
  italic(j = 2)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times") %>%
#--- font size ---#
  fontsize(i = 7, size = 16) %>%
#--- italicize ---#
  italic(j = 2) %>%
#--- bold ---#
  bold(j = 4)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times") %>%
#--- font size ---#
  fontsize(i = 7, size = 16) %>%
#--- italicize ---#
  italic(j = 2) %>%
#--- bold ---#
  bold(j = 4) %>%
#--- vertical text alignment ---#
  valign(i = ~ region == "Australasia", j = 4, valign = "top")

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Formatting

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- text color ---#
  color(i = 1, j = 2, "#fcba03") %>%
#--- background ---#
  bg(i = ~ y_2005 < 2e5, j = c("region"), bg = "grey") %>%
#--- font type ---#
  font(i = 5, j = ~ country_name + y_2005, fontname = "Times") %>%
#--- font size ---#
  fontsize(i = 7, size = 16) %>%
#--- italicize ---#
  italic(j = 2) %>%
#--- bold ---#
  bold(j = 4) %>%
#--- vertical text alignment ---#
  valign(i = ~ region == "Australasia", j = 4, valign = "top") %>%
#--- text direction ---#
  rotate(i = 1, j = 2, rotation = "tbrl")

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Borders: convenience functions

Here is a list of convenience functions that you can use to draw border lines on a table:

  • hline(): set horizontal borders
  • hline_bottom(): set bottom horizontal border
  • hline_top(): set top horizontal border
  • vline(): set vertical borders
  • vline_left(): set flextable left vertical borders
  • vline_right(): set flextable right vertical borders
  • border(): Set cell borders
  • border_inner(): set vertical & horizontal inner borders
  • border_inner_h(): set inner borders
  • border_inner_v(): set vertical inner borders
  • border_outer(): set outer borders
  • border_remove(): remove borders
  • fix_border_issues(): fix border issues when cell are merged
  • Use the selector syntax to specify where
  • Use the border = option along with fp_border() from the officer package to specify what kind of borders you would like to draw

Example

hline(
    i = 3, 
    j = 1:3, 
    border = fp_border( 
      color = "red", 
      style = "dotted", 
      width = 4 
    ) 
  ) 

fp_border() lets you specify the aesthetics of the borders you are drawing.


Syntax

ft %>%
  hline(
    i = 3, 
    j = 1:3, 
    border = fp_border(
      color = "red",
      style = "dotted",
      width = 4
    )
  ) 

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data
# A tibble: 10 Γ— 7
   country_name     region        y_1995   y_2005   y_2015 pop_ratio_10_15 date 
   <chr>            <chr>          <int>    <int>    <int>           <dbl> <chr>
 1 Australia        Australasia 18004882 20176844 23815995            1.18 2013…
 2 New Zealand      Australasia  3673400  4133900  4609400            1.12 2013…
 3 Papua New Guinea Melanesia    4616439  6498818  8682174            1.34 2013…
 4 Solomon Islands  Melanesia     375189   482486   612660            1.27 2013…
 5 Vanuatu          Melanesia     170612   217632   276438            1.27 2013…
 6 New Caledonia    Melanesia     193816   232250   269460            1.16 2013…
 7 French Polynesia Polynesia     231446   271060   291787            1.08 2013…
 8 Samoa            Polynesia     174902   188626   203571            1.08 2013…
 9 Tonga            Polynesia      99977   105633   106122            1.00 2013…
10 Tuvalu           Polynesia       9585     9912    10877            1.10 2013…

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove()

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2)) %>%
#--- borders of individual cells ---#
  border(i = 4, j = 2,
    border.top = fp_border(color = "red", width = 3),
    border.left = fp_border(color = "green", width = 3),
    border.right = fp_border(color = "black", width = 3),
    border.bottom = fp_border(color = "pink", width = 3)
  )

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2)) %>%
#--- borders of individual cells ---#
  border(i = 4, j = 2,
    border.top = fp_border(color = "red", width = 3),
    border.left = fp_border(color = "green", width = 3),
    border.right = fp_border(color = "black", width = 3),
    border.bottom = fp_border(color = "pink", width = 3)
  ) %>%
  #--- horizontal lines (inner) ---#  #--- horizontal lines (inner) ---#
  border_inner_h(border = fp_border(color = "black"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2)) %>%
#--- borders of individual cells ---#
  border(i = 4, j = 2,
    border.top = fp_border(color = "red", width = 3),
    border.left = fp_border(color = "green", width = 3),
    border.right = fp_border(color = "black", width = 3),
    border.bottom = fp_border(color = "pink", width = 3)
  ) %>%
  #--- horizontal lines (inner) ---#  #--- horizontal lines (inner) ---#
  border_inner_h(border = fp_border(color = "black")) %>%
  #--- vertical lines (inner) ---#  #--- vertical lines (inner) ---#
  border_inner_v(border = fp_border(color = "black"))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2)) %>%
#--- borders of individual cells ---#
  border(i = 4, j = 2,
    border.top = fp_border(color = "red", width = 3),
    border.left = fp_border(color = "green", width = 3),
    border.right = fp_border(color = "black", width = 3),
    border.bottom = fp_border(color = "pink", width = 3)
  ) %>%
  #--- horizontal lines (inner) ---#  #--- horizontal lines (inner) ---#
  border_inner_h(border = fp_border(color = "black")) %>%
  #--- vertical lines (inner) ---#  #--- vertical lines (inner) ---#
  border_inner_v(border = fp_border(color = "black")) %>%
  border_remove()

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Demonstration: Borders

tab_data %>%
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>%
#--- remove all borders ---#
  border_remove() %>%
#--- horizontal lines ---#
  hline(i = 3, j = 1:3, border = fp_border(color = "red")) %>%
#--- horizontal line at the bottom ---#
  hline_bottom(j = 3:4, border = fp_border(color = "green")) %>%
#--- horizontal line at the top ---#
  hline_top(j = 1:3, border = fp_border(color = "orange")) %>%
#--- vertical lines ---#
  vline(border = fp_border(color = "orange")) %>%
#--- vertical on the left edge  ---#
  vline_left(border = fp_border(color = "grey", width = 2)) %>%
#--- vertical on the right edge  ---#
  vline_right(border = fp_border(color = "red", width = 2)) %>%
#--- borders of individual cells ---#
  border(i = 4, j = 2,
    border.top = fp_border(color = "red", width = 3),
    border.left = fp_border(color = "green", width = 3),
    border.right = fp_border(color = "black", width = 3),
    border.bottom = fp_border(color = "pink", width = 3)
  ) %>%
  #--- horizontal lines (inner) ---#  #--- horizontal lines (inner) ---#
  border_inner_h(border = fp_border(color = "black")) %>%
  #--- vertical lines (inner) ---#  #--- vertical lines (inner) ---#
  border_inner_v(border = fp_border(color = "black")) %>%
  border_remove() %>%
#--- the outer lines ---#
  border_outer(border = fp_border(color = "red", width = 4))

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Headers and footers

You can use these functions to add a row to the top or the bottom of a table:

  • add_header_row()
  • add_footer_row()


Syntax

add_*_row(flextable, 
  values = vector of characters, 
  colwidths = vector of integers 
)


Example

add_header_row(
  values = c("3-column label", "1-column label"),
  colwidths = c(3, 1)
)

This code would insert a row where β€œ3-column label” spans for three columns and β€œ1-column label” spans for one column.


Note

You might want to use footnote() to create footnotes instead of add_footer_rows, as it allows you to generate reference symbols at the same time.

ft %>% 
  add_header_row(
    values = c("3-column label", "1-column label"),
    colwidths = c(3, 1)
  ) %>% 
  align(align = "center", part = "header") %>% 
  autofit() %>% 
  add_footer_row(
    values = "4-column footnote, which is made longer to show it spans across the entire columns.",
    colwidths = 4
  )

3-column label

1-column label

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

4-column footnote, which is made longer to show it spans across the entire columns.

Explanation

footnote() lets you add footnotes with reference symbols for each of them.


Syntax

ft %>%
  footnote(
    i = 1, j = 1:2, part = "header",
    value = as_paragraph(
      c(
        "This is footnote 1",
        "This is footnote 2"
      )
    ),
    ref_symbols = c("++", "**")
  )

country_name++

region**

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

++This is footnote 1

**This is footnote 2

  • nth element in value is associated with nth value in ref_symbols
  • use as_paragraph(c()) for value

set_header_labels() lets you re-label existing header labels using a named list.


Syntax

set_header_labels(
  values = list( 
    existing name 1 = "new name 1", 
    existing name 2 = "new name 2" 
  )
)


Example

ft %>%
  set_header_labels(
    values = list(
      country_name = "Country Name",
      region = "Region"
    )
  ) %>% 
  autofit() 

Country Name

Region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Syntax

delete_part(flextable, part = "header")


The default is to delete the header.

Example

ft %>% 
  delete_part()

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

You can still (or have to) use the original variable names from the dataset for selectors even after you deleter the header:

tab_data %>%  
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>% 
  delete_part() %>% 
  hline(
    i = 3, 
    j = ~ country_name + region, 
    border = fp_border(color = "red", style = "dotted", width = 4 
    ) 
  )  

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Layout

List of functions

Here is a list of functions you can use to change the layout of a table:

  • merge_at(): Merge flextable cells into a single one
  • merge_h(): Merge flextable cells horizontally
  • merge_h_range(): rowwise merge of a range of columns
  • merge_v(): Merge flextable cells vertically
  • height(), height_all(): Set flextable rows height
  • width(): Set flextable columns width
  • hrule(): Set flextable rule for rows heights
  • autofit(): Adjusts cell widths and heights
  • fit_to_width(): fit a flextable to a maximum width
  • as_grouped_data(): grouped data transformation

Use the selector syntax to specify where just like the other functions we have seen. We will look at merge_v(), autofit(), and width().


Note

I have not encountered cases where I need to merge cells horizontally. It works in a similar manner to the way merge_v() works except that it works on rows instead of columns.

merge_v() merges vertically the adjacent cells with the same values. It does not accept i (rows) argument.

Before

ft 

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

After

ft %>%  merge_v(j = ~ region)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

375,189

482,486

Vanuatu

170,612

217,632

New Caledonia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

174,902

188,626

Tonga

99,977

105,633

Tuvalu

9,585

9,912

autofit() adjust the height and width of cells .

Before

tab_data %>%
  mutate(country_name = ifelse(country_name == "Australia", "super long country name .......... bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh", country_name)) %>% 
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>% 
  merge_v(j = ~ region)

country_name

region

y_1995

y_2005

super long country name .......... bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh

Australasia

18,004,882

20,176,844

New Zealand

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

375,189

482,486

Vanuatu

170,612

217,632

New Caledonia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

174,902

188,626

Tonga

99,977

105,633

Tuvalu

9,585

9,912

After

tab_data %>%
  mutate(country_name = ifelse(country_name == "Australia", "super long country name .......... bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh", country_name)) %>% 
  flextable(
    col_keys = c("country_name", "region", "y_1995", "y_2005")
  ) %>% 
  merge_v(j = ~ region) %>% autofit()

country_name

region

y_1995

y_2005

super long country name .......... bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh bluh

Australasia

18,004,882

20,176,844

New Zealand

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

375,189

482,486

Vanuatu

170,612

217,632

New Caledonia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

174,902

188,626

Tonga

99,977

105,633

Tuvalu

9,585

9,912

It adjusted the width of the 1st column so that more texts are displayed in a single row. But, the width of the entire table does not go over the limit of the paper.

width() set the width of columns to the length you specify.

Before

ft %>%
  autofit()

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

After

ft %>% 
  autofit() %>% 
  width(j = ~ country_name, width = 1)

country_name

region

y_1995

y_2005

Australia

Australasia

18,004,882

20,176,844

New Zealand

Australasia

3,673,400

4,133,900

Papua New Guinea

Melanesia

4,616,439

6,498,818

Solomon Islands

Melanesia

375,189

482,486

Vanuatu

Melanesia

170,612

217,632

New Caledonia

Melanesia

193,816

232,250

French Polynesia

Polynesia

231,446

271,060

Samoa

Polynesia

174,902

188,626

Tonga

Polynesia

99,977

105,633

Tuvalu

Polynesia

9,585

9,912

Output

We can save the table in various formats.

  • save_as_docx(): docx (WORD)
  • save_as_pptx(): pptx (Power Point)
  • save_as_image(): image (png, pdf, jpeg) with help from the webshot2 package

Let’s create a table for demonstration:


Power Point

save_as_pptx(table_tmep, path = "temp_table.pptx")

Image

I do not really recommend this option. It is hard to configure the output.

First install the webshot2 package.

#--- install ---#
remotes::install_github("rstudio/webshot2") 

#--- library ---#
library(webshot2)


png

save_as_image(table_tmep, path = "temp_table.png") 


pdf

save_as_image(table_tmep, path = "temp_table.pdf", zoom = 10, webshot = "webshot2") 

:::