Title: | Extensions for 'Flextable' |
---|---|
Description: | Build display tables easily by extending the functionality of the 'flextable' package. Features include spanning header, grouping rows, parsing markdown and so on. |
Authors: | Atsushi Yasumoto [aut, cph, cre] , David Gohel [ctb], Romain François [ctb] , Tatsuya Shima [ctb] |
Maintainer: | Atsushi Yasumoto <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.6.4 |
Built: | 2024-11-06 03:26:36 UTC |
Source: | https://github.com/atusy/ftextra |
This is a convenient function to let users create flextable bindings from any objects. Users should consult documentation of corresponding method to understand the details and see what arguments can be used.
## S3 method for class 'grouped_df' as_flextable( x, groups_to = c("titles", "merged", "asis"), groups_pos = c("left", "asis"), groups_arrange = NULL, ... ) ## S3 method for class 'data.frame' as_flextable(x, col_keys = names(x), ...)
## S3 method for class 'grouped_df' as_flextable( x, groups_to = c("titles", "merged", "asis"), groups_pos = c("left", "asis"), groups_arrange = NULL, ... ) ## S3 method for class 'data.frame' as_flextable(x, col_keys = names(x), ...)
x |
object to be transformed as flextable |
groups_to |
One of |
groups_pos |
When |
groups_arrange |
|
... |
arguments for custom methods |
col_keys |
columns names/keys to display. If some column names are not in the dataset, they will be added as blank columns by default. |
Other as_flextable methods:
as_flextable.data.frame()
,
as_flextable.gam()
,
as_flextable.glm()
,
as_flextable.grouped_data()
,
as_flextable.htest()
,
as_flextable.kmeans()
,
as_flextable.lm()
,
as_flextable.merMod()
,
as_flextable.pam()
,
as_flextable.summarizor()
,
as_flextable.table()
,
as_flextable.tabular()
,
as_flextable.tabulator()
,
as_flextable.xtable()
# For grouped_df grouped_df <- iris %>% dplyr::group_by(Species) %>% dplyr::slice(1, 2) as_flextable(grouped_df, groups_to = "titles") as_flextable(grouped_df, groups_to = "titles", hide_grouplabel = TRUE) as_flextable(grouped_df, groups_to = "merged") as_flextable(grouped_df, groups_to = "asis") # For data.frame iris %>% head() %>% as_flextable()
# For grouped_df grouped_df <- iris %>% dplyr::group_by(Species) %>% dplyr::slice(1, 2) as_flextable(grouped_df, groups_to = "titles") as_flextable(grouped_df, groups_to = "titles", hide_grouplabel = TRUE) as_flextable(grouped_df, groups_to = "merged") as_flextable(grouped_df, groups_to = "asis") # For data.frame iris %>% head() %>% as_flextable()
Parse markdown cells and returns the "paragraph" object.
as_paragraph_md( x, auto_color_link = "blue", md_extensions = NULL, pandoc_args = NULL, metadata = rmarkdown::metadata, replace_na = "", .from = "markdown+autolink_bare_uris-raw_html-raw_attribute", .footnote_options = NULL, ... )
as_paragraph_md( x, auto_color_link = "blue", md_extensions = NULL, pandoc_args = NULL, metadata = rmarkdown::metadata, replace_na = "", .from = "markdown+autolink_bare_uris-raw_html-raw_attribute", .footnote_options = NULL, ... )
x |
A character vector. |
auto_color_link |
A color of the link texts. |
md_extensions |
Pandoc's extensions. Although it is prefixed with "md", extensions for any
formats specified to |
pandoc_args |
Additional command line options to pass to pandoc |
metadata |
A list of metadata, typically the parsed result of the YAML front matter
(default: |
replace_na |
A value to replace |
.from |
Pandoc's |
.footnote_options |
Options for footnotes generated by |
... |
Arguments passed to internal functions. |
if (rmarkdown::pandoc_available("2.0.6")) { library(flextable) ft <- flextable( data.frame( x = c("**foo** bar", "***baz***", "*qux*"), stringsAsFactors = FALSE ) ) ft <- compose(ft, j = "x", i = 1:2, value = as_paragraph_md(x)) autofit(ft) }
if (rmarkdown::pandoc_available("2.0.6")) { library(flextable) ft <- flextable( data.frame( x = c("**foo** bar", "***baz***", "*qux*"), stringsAsFactors = FALSE ) ) ft <- compose(ft, j = "x", i = 1:2, value = as_paragraph_md(x)) autofit(ft) }
Format character columns as markdown text
colformat_md( x, j = where(is.character), part = c("body", "header", "all"), auto_color_link = "blue", md_extensions = NULL, pandoc_args = NULL, metadata = rmarkdown::metadata, replace_na = "", .from = "markdown+autolink_bare_uris-raw_html-raw_attribute", .footnote_options = footnote_options(), .sep = "\n\n" )
colformat_md( x, j = where(is.character), part = c("body", "header", "all"), auto_color_link = "blue", md_extensions = NULL, pandoc_args = NULL, metadata = rmarkdown::metadata, replace_na = "", .from = "markdown+autolink_bare_uris-raw_html-raw_attribute", .footnote_options = footnote_options(), .sep = "\n\n" )
x |
A |
j |
Columns to be treated as markdown texts.
Selection can be done by the semantics of |
part |
One of "body", "header", and "all". If "all", formatting proceeds in the order of "header" and "body". |
auto_color_link |
A color of the link texts. |
md_extensions |
Pandoc's extensions. Although it is prefixed with "md", extensions for any
formats specified to |
pandoc_args |
Additional command line options to pass to pandoc |
metadata |
A list of metadata, typically the parsed result of the YAML front matter
(default: |
replace_na |
A value to replace |
.from |
Pandoc's |
.footnote_options |
Options for footnotes generated by |
.sep |
A separator of paragraphs (default: |
if (rmarkdown::pandoc_available("2.0.6")) { d <- data.frame( x = c("**bold**", "*italic*"), y = c("^superscript^", "~subscript~"), z = c("***^ft^~Extra~** is*", "*Cool*") ) colformat_md(flextable::flextable(d)) }
if (rmarkdown::pandoc_available("2.0.6")) { d <- data.frame( x = c("**bold**", "*italic*"), y = c("^superscript^", "~subscript~"), z = c("***^ft^~Extra~** is*", "*Cool*") ) colformat_md(flextable::flextable(d)) }
Configure options for footnotes.
footnote_options( ref = c("1", "a", "A", "i", "I", "*"), prefix = "", suffix = "", start = 1L, max = 26L, inline = FALSE, sep = "; " )
footnote_options( ref = c("1", "a", "A", "i", "I", "*"), prefix = "", suffix = "", start = 1L, max = 26L, inline = FALSE, sep = "; " )
ref |
A string or a function that defines symbols of footnote references.
If the value is string, it must be one of the "1", "a", "A", "i", "I", or
"*". If a function, keep in mind this is an experimental feature. It
receives 3 parameters ( |
prefix , suffix
|
Pre- and suf-fixes for |
start |
A starting number of footnotes. |
max |
A max number of footnotes used only when |
inline |
whether to add footnote on same line as previous footnote or not |
sep |
used only when inline = TRUE, character string to use as a separator between footnotes. |
An environment
# A examole flextable with unprocessed markdown footnotes ft <- flextable(tibble::tibble( "header1^[note a]" = c("x^[note 1]", "y"), "header2" = c("a", "b^[note 2]") )) # Render all footnotes in the same format. if (rmarkdown::pandoc_available("2.0.6")) { ft %>% colformat_md( part = "all", .footnote_options = footnote_options("1", start = 1L) ) } # Use a user-defined function to format footnote symbols # Skipped to reduce build time if (FALSE) { # a function to format symbols of footnote references ref <- function(n, part, footer) { # Change symbols by context # - header: letters (a, b, c, ...) # - body: integers (1, 2, 3, ...) s <- if (part == "header") { letters[n] } else { as.character(n) } # Suffix symbols with ": " (a colon and a space) in the footer if (footer) { return(paste0(s, ":\\ ")) } # Use superscript in the header and the body return(paste0("^", s, "^")) } # apply custom format of symbols ft %>% # process header first colformat_md( part = "header", .footnote_options = footnote_options(ref = ref) ) %>% # process body next colformat_md( part = "body", .footnote_options = footnote_options(ref = ref) ) %>% # tweak width for visibility flextable::autofit(add_w = 0.2) }
# A examole flextable with unprocessed markdown footnotes ft <- flextable(tibble::tibble( "header1^[note a]" = c("x^[note 1]", "y"), "header2" = c("a", "b^[note 2]") )) # Render all footnotes in the same format. if (rmarkdown::pandoc_available("2.0.6")) { ft %>% colformat_md( part = "all", .footnote_options = footnote_options("1", start = 1L) ) } # Use a user-defined function to format footnote symbols # Skipped to reduce build time if (FALSE) { # a function to format symbols of footnote references ref <- function(n, part, footer) { # Change symbols by context # - header: letters (a, b, c, ...) # - body: integers (1, 2, 3, ...) s <- if (part == "header") { letters[n] } else { as.character(n) } # Suffix symbols with ": " (a colon and a space) in the footer if (footer) { return(paste0(s, ":\\ ")) } # Use superscript in the header and the body return(paste0("^", s, "^")) } # apply custom format of symbols ft %>% # process header first colformat_md( part = "header", .footnote_options = footnote_options(ref = ref) ) %>% # process body next colformat_md( part = "body", .footnote_options = footnote_options(ref = ref) ) %>% # tweak width for visibility flextable::autofit(add_w = 0.2) }
Span the header based on delimiters
span_header(x, sep = "[_.]", theme_fun = NULL, ...)
span_header(x, sep = "[_.]", theme_fun = NULL, ...)
x |
A |
sep |
Separator between columns. If character, If numeric, |
theme_fun |
A flextable theme function.
When |
... |
Passed to |
split_header
is a rename of separate_header
and the latter will be removed in the future release.
iris %>% flextable() %>% span_header()
iris %>% flextable() %>% span_header()
Split the header based on delimiters
split_header(x, sep = "[_.]", theme_fun = NULL, ...) separate_header(x, sep = "[_.]", theme_fun = NULL, ...)
split_header(x, sep = "[_.]", theme_fun = NULL, ...) separate_header(x, sep = "[_.]", theme_fun = NULL, ...)
x |
A |
sep |
Separator between columns. If character, If numeric, |
theme_fun |
A flextable theme function.
When |
... |
Passed to |
split_header
is a rename of separate_header
and the latter will be removed in the future release.
iris %>% flextable() %>% separate_header()
iris %>% flextable() %>% separate_header()
col_keys
Specify blank columns easily via col_keys
with_blanks(after = NULL, before = NULL)
with_blanks(after = NULL, before = NULL)
after , before
|
Blank columns are added after/before the selected columns.
Selections can be done by the semantics of |
iris %>% as_flextable(col_keys = with_blanks(dplyr::ends_with("Width")))
iris %>% as_flextable(col_keys = with_blanks(dplyr::ends_with("Width")))