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() will merge adjacent duplicated cells for each row of the selection.
merge_h_range is close to the previous one but merge all colums between a range of columns.
merge_at() will merge cells for a given continuous selection of rows and cells. The result is a single cell.
Note that the content that is rendered is not the result of the concatenation of each paragraphs contained into the merge cells. Only one paragraph will be kept, the top left one of all merge cells.
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.
By default, table width is fixed. This setting allows to have the same rendering with Word, HTML and PowerPoint formats.
You can use another setting with function
set_table_properties(). Alternative is ‘autofit’, an algorithm implemented by HTML and Word. It allows to size the table along an available width.
This oonly applies when layout is “fixed”
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.
autofitwas not my biggest naming idea as users are thinking it is the ‘Microsoft Word feature’ (see set_table_properties)
ft <- autofit(ft_base, add_w = 0, add_h = 0) dim(ft) #> $widths #> mpg cyl disp hp drat wt qsec vs #> 0.5089948 0.3719030 0.4881886 0.4523067 0.4998259 0.6012121 0.6012121 0.3287037 #> am gear carb #> 0.4046111 0.5044104 0.5032643 #> #> $heights #>  0.3220916 0.2800383 0.2800383 0.2800383 0.2800383 0.2800383 0.2800383 ft
Soft returns (a line break in a paragraph) support : function
dim_prettydo not support soft returns and may return wrong results (will be considered as "").
height() let you control dimensions of a flextable.
height_all() is an helper function to set the same height to each part of the table.