By default, the displayed content of each cell will be the result of a simple formatting. The content can also be composed as the result of a concatenation of several chunks.
formatter functions to define how columns content will be display (e.g. to define the number of decimal). This is a column-wise operation.
User can also have more control over displayed content by using function
compose. The function enable to define the elements composing the paragraph and their respective formats. It can also be used to mix text chunks and images.
Theses are high level functions that should satisfy most of the usual needs. They can be used to define the formatting of one or more columns and eventually on a subset of rows.
Each accept a
suffix argument that can be used to add a currency symbol for example. Also they all have
na_str argument (defaut to ""), the string to use when data are not available.
num_keys <- c("mpg", "disp", "drat", "wt", "qsec") int_keys <- c("cyl", "hp", "vs", "am", "gear", "carb") ft <- colformat_num(x = ft, col_keys = num_keys, big.mark = ",", digits = 2, na_str = "missing") #> Warning in colformat_num.flextable(x = ft, col_keys = num_keys, big.mark = #> ",", : argument col_keys is deprecated in favor of argument j ft <- colformat_int(x = ft, col_keys = int_keys, big.mark = ",") #> Warning in colformat_int.flextable(x = ft, col_keys = int_keys, big.mark = ","): #> argument col_keys is deprecated in favor of argument j autofit(ft)
Their names is
col_format_*as the first version was not working with rows subset. This limitation is no more true and the functions should then be named
format_num… as they can operate on rows and columns.
It is possible to define the functions that will be used to format the data.frame values into strings.
set_formatter set column formatter functions.
flextable content can be defined with function
It lets user control the formated content at the cell level of the table. It is possible to define a content for a row subset and a column as well as on the whole column. One can mix images and text (but not with PowerPoint because PowerPoint can not do it).
Below the starting point of next illustrations:
myft <- flextable( head(mtcars), col_keys = c("am", "separator", "gear", "mpg", "drat", "qsec" )) myft <- bold(myft, part = "header") myft <- border(myft, border = fp_border( width = 0), border.top = fp_border(), border.bottom = fp_border(), part = "all") myft <- align(myft, align = "right", part = "all" ) myft <- border(myft, j = ~ separator, border = fp_border(width=0), part = "all") myft <- width(myft, j = ~ separator, width = .1) myft
The function requires a call to
as_paragraph which will concatenate text or images chunks as a paragraph.
The following example shows how to control the format of displayed values and how to associate them with specific text formatting properties (bold red text):
With that system, it’s easy to concatenate multiple values:
Or to define specific title headers:
Using images in flextable is not supported when output format is PowerPoint. This is not a choice nor a unimplemented feature. This is because PowerPoint is not able to embed images in a table cell. That’s a PowerPoint limitation.
If beeing able to display images in PowerPoint table is important to you, you can use the
plot function or the
save_as_image and embed the result in the PowerPoint. You will of course loose ability to edit the table in PowerPoint.
compose supports images insertion. Use function
compose supports mini barplots insertion. Use function
compose supports mini linerange insertion. Use function
as_sup are special functions that can be used together. They set a value as bold, italic, subscripted or superscripted. This is particularly useful when the headers need complex formatting.
Adding footnotes is possible by using function
compose but it requires a two step approach (add the reference symbol and add the corresponding footnote in the footer part).
To make the process simpler, use function
footnote that will ease the addition of footnotes.
A cell is made of one single paragraph of text. Paragraphs can contain several chunks of text with different formatting but also images. (see keypoints)
When designed, flextable was not supposed to support soft return (a new line in a paragraph). As requested by some users, some workarounds have been implemented to let add some
\n as soft break in paragraphs.
With HTML, it works fine. But with Word or PowerPoint it comes with limitations/issues (we are working on it):