flextable
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
flextable
package 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
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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Install the following packages and library them.
#--- 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:
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)
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 |
Many functions let you choose specifically where you apply changes. Those functions have
i
for selecting rows
j
for selecting columns
You can use any combinations of the reference methods for i
and j
.
We will be building on ft
created below:
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
Example
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.
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 |
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 tablefooter
: the footer part of the tablebody
: the body part of the tableall
: the body and the header parts of the tablecountry_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
.
You can use the style()
function to change the style and format of a table.
Syntax
We can use fp_*()
functions from the officer
package to specify the style of texts, paragraphs, and cells.
pr_t = fp_text()
: format textspr_p = fp_par()
: format paragraphspr_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
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 cellscells
: rectangular boxes that contain paragraphs and texts insidecountry_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
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
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 |
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 alignmentbg()
: Set background colorfont()
: Set fontfontsize()
: Set font sizeitalic()
: Set italic fontbold()
: Set bold fontcolor()
: Set font colorpadding()
: Set paragraph paddingsvalign()
: Set vertical alignmentrotate()
: rotate cell textempty_blanks()
: make blank columns as transparentcountry_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 |
# 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β¦
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Here is a list of convenience functions that you can use to draw border lines on a table:
hline()
: set horizontal bordershline_bottom()
: set bottom horizontal borderhline_top()
: set top horizontal bordervline()
: set vertical bordersvline_left()
: set flextable left vertical bordersvline_right()
: set flextable right vertical bordersborder()
: Set cell bordersborder_inner()
: set vertical & horizontal inner bordersborder_inner_h()
: set inner bordersborder_inner_v()
: set vertical inner bordersborder_outer()
: set outer bordersborder_remove()
: remove bordersfix_border_issues()
: fix border issues when cell are mergedborder =
option along with fp_border()
from the officer
package to specify what kind of borders you would like to drawExample
fp_border()
lets you specify the aesthetics of the borders you are drawing.
Syntax
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 |
# 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β¦
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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
Example
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
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 |
n
th element in value
is associated with n
th value in ref_symbols
as_paragraph(c())
for value
set_header_labels()
lets you re-label existing header labels using a named list.
Syntax
Example
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
The default is to delete the header.
Example
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 |
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 onemerge_h()
: Merge flextable cells horizontallymerge_h_range()
: rowwise merge of a range of columnsmerge_v()
: Merge flextable cells verticallyheight()
, height_all()
: Set flextable rows heightwidth()
: Set flextable columns widthhrule()
: Set flextable rule for rows heightsautofit()
: Adjusts cell widths and heightsfit_to_width()
: fit a flextable to a maximum widthas_grouped_data()
: grouped data transformationUse 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
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
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
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
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 |
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
packageLetβs create a table for demonstration:
Power Point
I do not really recommend this option. It is hard to configure the output.
First install the webshot2
package.
png
:::