flextable
layout can be managed with few functions.
library(flextable)
library(officer)
library(magrittr)
merge_v
will merge adjacent duplicated cells for each column of the selection.
select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- regulartable(iris[46:55,], col_keys = select_columns) %>%
merge_v(~ Species + Petal.Width )
tabwid(myft)
merge_h
will merge adjacent duplicated cells for each row of the selection.
select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- regulartable(head(mtcars, n = 10 ) ) %>%
merge_h( ) %>%
border(border = fp_border(), part = "all") # and add borders
tabwid(myft)
select_columns <- c("Species", "Petal.Length", "Petal.Width")
myft <- regulartable(head(mtcars, n = 6 ) ) %>%
merge_at( i = 1:3, j = 1:3) %>%
border(border = fp_border(), part = "all") # and add borders
tabwid(myft)
If you want to get rid of all merging (i.e. for development purposes), use merge_none
:
tabwid(myft %>% merge_none())
Parameter col_keys
define the variables to display and their order.
data <- iris[c(1:3, 51:53, 101:104),]
regulartable(data, col_keys = c("Species", "Sepal.Length", "Petal.Length") ) %>%
theme_booktabs() %>%
tabwid()
If parameter col_keys
has variables that are not existing in the dataset, they will be considered as blank columns and can be used as separators (in fact they can be use as you want, there is only no mapping of data associated).
regulartable(data = data, col_keys = c("Species", "col_1", "Sepal.Length", "Petal.Length") ) %>%
theme_vanilla() %>% autofit() %>%
border(j=2, border = fp_border(width=0), part = "all") %>%
tabwid()
col_keys
default values are the names of the data.frame used to fill the flextable.
Use set_header_labels
to replace labels of the bottom row of header.
ft <- regulartable( head( iris ) ) %>%
set_header_labels(Sepal.Length = "Sepal",
Sepal.Width = "Sepal", Petal.Length = "Petal",
Petal.Width = "Petal", Species = "Species" )
ft %>% theme_vanilla() %>% autofit() %>% tabwid()
Use add_header
to add an header row.
ft <- ft %>%
add_header(Sepal.Length = "length",
Sepal.Width = "width", Petal.Length = "length",
Petal.Width = "width", Species = "Species", top = FALSE )
ft %>% theme_vanilla() %>% autofit() %>% tabwid()
ft <- ft %>%
add_header(Sepal.Length = "Inches",
Sepal.Width = "Inches", Petal.Length = "Inches",
Petal.Width = "Inches", Species = "Species", top = TRUE )
# merge identical cells
ft <- ft %>% merge_h(part = "header") %>% merge_v(part = "header")
ft %>% theme_vanilla() %>% autofit() %>% tabwid()
Use set_header_df
with a data.frame as parameter. Columns of the dataset will be transposed and joined using a key column.
Variable col_keys
define key values to match with flextable column keys (defined by argument col_keys
of flextable
function).
This key column will not be displayed. Other variables will added as rows. Note that variables names are not displayed.
typology <- data.frame(
col_keys = c( "Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width", "Species" ),
type = c("double", "double", "double", "double", "factor"),
what = c("Sepal", "Sepal", "Petal", "Petal", "Species"),
measure = c("Length", "Width", "Length", "Width", "Species"),
stringsAsFactors = FALSE )
typology %>% flextable() %>% theme_vanilla() %>% autofit() %>% tabwid()
Then use set_header_df
with parameter key
that specifies name of the column used to permform the join. Order of columns matters, first column will be first row, second one will be the second row, etc.
regulartable( head( iris ) ) %>%
set_header_df( mapping = typology, key = "col_keys" ) %>%
merge_h(part = "header") %>% merge_v(part = "header") %>%
theme_vanilla() %>% autofit() %>% tabwid()
The default sizes of flextable columns and rows are calculated with a simple algorithm. This will drive to inadequate rows heights and columns widths in some cases (when data values are wider than headers). You can use function dim
to get flextable dimensions.
ft_base <- regulartable(head(iris)) %>%
theme_tron_legacy()
tabwid(ft_base)
dim(ft_base)
#> $widths
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 0.75 0.75 0.75 0.75 0.75
#>
#> $heights
#> [1] 0.25 0.25 0.25 0.25 0.25 0.25 0.25
Function dim_pretty
is computing optimized widths and heights.
dim_pretty(ft_base)
#> $widths
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 0.9460630 0.8688196 0.9074752 0.8302318 0.5991121
#>
#> $heights
#> [1] 0.2044994 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366
This function is time consuming and should be used sparingly.
Function autofit
optimises widths and heights of the flextable. This function is almost always to be called once when using flextable, it makes compact tables.
ft <- ft_base %>%
autofit(add_w = 0, add_h = 0)
dim(ft)
#> $widths
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 0.9460630 0.8688196 0.9074752 0.8302318 0.5991121
#>
#> $heights
#> [1] 0.2044994 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366 0.1756366
tabwid(ft)
Function width
and height
let you control dimensions of a flextable.
ft <- ft_base %>% autofit() %>%
width(j = ~ Species, width = 2) %>%
height( height = .4, part = "all" )
tabwid(ft)