Title: | Extensions for 'Flextable' |
Version: | 0.6.4 |
Date: | 2024-05-11 |
Maintainer: | Atsushi Yasumoto <atusy.rpkg@gmail.com> |
Description: | Build display tables easily by extending the functionality of the 'flextable' package. Features include spanning header, grouping rows, parsing markdown and so on. |
License: | MIT + file LICENSE |
URL: | https://ftextra.atusy.net, https://github.com/atusy/ftExtra |
BugReports: | https://github.com/atusy/ftExtra/issues |
Imports: | dplyr (≥ 1.0.0), jsonlite, flextable (≥ 0.9.5), tidyr, purrr (≥ 1.0.0), magrittr, rmarkdown, rlang, stringr, tibble, tidyselect (≥ 1.1.0), xfun, yaml, vctrs |
Suggests: | testthat (≥ 2.1.0), knitr, qpdf |
VignetteBuilder: | knitr |
Encoding: | UTF-8 |
Language: | en-US |
RoxygenNote: | 7.3.1 |
SystemRequirements: | pandoc (>= 2.0.6) - http://pandoc.org |
Config/testthat/edition: | 3 |
NeedsCompilation: | no |
Packaged: | 2024-05-10 00:19:48 UTC; atusy |
Author: | Atsushi Yasumoto |
Repository: | CRAN |
Date/Publication: | 2024-05-10 03:40:03 UTC |
Method to transform objects into flextables
Description
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.
Usage
## 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), ...)
Arguments
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. |
See Also
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()
Examples
# 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()
Convert a character vector into markdown paragraph(s)
Description
Parse markdown cells and returns the "paragraph" object.
Usage
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,
...
)
Arguments
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. |
Examples
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
Description
Format character columns as markdown text
Usage
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"
)
Arguments
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: |
Examples
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))
}
Options for footnotes
Description
Configure options for footnotes.
Usage
footnote_options(
ref = c("1", "a", "A", "i", "I", "*"),
prefix = "",
suffix = "",
start = 1L,
max = 26L,
inline = FALSE,
sep = "; "
)
Arguments
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. |
Value
An environment
Examples
# 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)
}
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
- flextable
- magrittr
Span the header based on delimiters
Description
Span the header based on delimiters
Usage
span_header(x, sep = "[_.]", theme_fun = NULL, ...)
Arguments
x |
A |
sep |
Separator between columns. If character, If numeric, |
theme_fun |
A flextable theme function.
When |
... |
Passed to |
Note
split_header
is a rename of separate_header
and the latter will be removed in the future release.
Examples
iris %>%
flextable() %>%
span_header()
Split the header based on delimiters
Description
Split the header based on delimiters
Usage
split_header(x, sep = "[_.]", theme_fun = NULL, ...)
separate_header(x, sep = "[_.]", theme_fun = NULL, ...)
Arguments
x |
A |
sep |
Separator between columns. If character, If numeric, |
theme_fun |
A flextable theme function.
When |
... |
Passed to |
Note
split_header
is a rename of separate_header
and the latter will be removed in the future release.
Examples
iris %>%
flextable() %>%
separate_header()
Specify blank columns easily via col_keys
Description
Specify blank columns easily via col_keys
Usage
with_blanks(after = NULL, before = NULL)
Arguments
after , before |
Blank columns are added after/before the selected columns.
Selections can be done by the semantics of |
Examples
iris %>%
as_flextable(col_keys = with_blanks(dplyr::ends_with("Width")))