flextable layout can be easily managed. A set of functions will let you merge cells, add title rows, add footer notes, change the withs or heights.
col_keys of function
flextable define the variables to display and their order.
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).
col_keys default values are the names of the data.frame used to fill the flextable.
set_header_labels() to replace labels of the bottom row of header. When the flextable is created, their values are the column names of the data.frame.
New header rows can be added at the top or bottom of the header. This part in documented in part Manage headers and footers.
Under the hood, the names are in a single row data.frame associated with the header part of the flextable. You can add new rows later, they will be binded to that data.frame.
To illustrate functions, we will use a basic flextable example:
merge_v() will merge adjacent duplicated cells for each column of the selection.
merge_h_range is close to the previous one but merge all colums between a range of columns.
If you want to get rid of all merging (i.e. for development purposes), use
When cells are merged, the rendered borders will be those of the first cell. If a column is made of three merged cells, the bottom border that will be seen will be the bottom border of the first cell in the column. From a user point of view, this is wrong, the bottom should be the one defined for cell 3. Function
fix_border_issues is trying to fix that issue.
The default sizes of flextable columns and rows are set by default values. This will drive to inadequate rows heights and columns widths in some cases. You can use function
dim to get flextable dimensions.
dim_pretty() is computing optimized widths and heights.
autofit() optimises widths and heights of the flextable. This function is almost always to be called once when using flextable objects, it makes compact tables.
ft <- autofit(ft_base, add_w = 0, add_h = 0) dim(ft) #> $widths #> mpg cyl disp hp drat wt qsec #> 0.7116790 0.6090585 0.8142994 0.8142994 0.6090585 0.6090585 0.7116790 #> vs am gear carb #> 0.6090585 0.6090585 0.6090585 0.6090585 #> #> $heights #>  0.3220916 0.2800383 0.2800383 0.2800383 0.2800383 0.2800383 0.2800383 ft
Soft returns support : function
dim_prettydo not support soft returns and may return wrong results (will be considered as "").
Package flextable does not support data transformation. A grouped dataset is then needed (and tidy). This has to be done prior to the creation of the object.
as_grouped_data will modify data structure so that it will be easy to manage grouped data representation. Repeated consecutive values of group columns will be used to define the title of the groups and will be added as a row title.
Let’s have an example with aggragated data from dataset
data_CO2 <- dcast(as.data.table(CO2), Treatment + conc ~ Type, value.var = "uptake", fun.aggregate = mean) head(data_CO2) #> Treatment conc Quebec Mississippi #> 1: nonchilled 95 15.26667 11.30000 #> 2: nonchilled 175 30.03333 20.20000 #> 3: nonchilled 250 37.40000 27.53333 #> 4: nonchilled 350 40.36667 29.90000 #> 5: nonchilled 500 39.60000 30.60000 #> 6: nonchilled 675 41.50000 30.53333
as_grouped_data will restructure the dataset:
data_CO2 <- as_grouped_data(x = data_CO2, groups = c("Treatment")) head(data_CO2) #> Treatment conc Quebec Mississippi #> 1 nonchilled NA NA NA #> 3 <NA> 95 15.26667 11.30000 #> 4 <NA> 175 30.03333 20.20000 #> 5 <NA> 250 37.40000 27.53333 #> 6 <NA> 350 40.36667 29.90000 #> 7 <NA> 500 39.60000 30.60000
The result is suitable for method
as_flextable. A call to this function and few formatting operations are producing the following result:
It’s now easier to customize the rendering. Let’s format column
conc as an integer column:
Now let’s add nice bars before displaying the figures:
zz <- zz %>% compose(i = ~ is.na(Treatment), j = "Quebec", value = as_paragraph( minibar(Quebec), " ", as_chunk(Quebec, formater = function(x) sprintf("%.01f", x)) ) ) %>% compose(i = ~ is.na(Treatment), j = "Mississippi", value = as_paragraph( minibar(Mississippi), " ", as_chunk(Mississippi, formater = function(x) sprintf("%.01f", x) ) ) ) %>% align(j = 2:3, align = "left") zz
And finally, add a footnote in the footer part: