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 grouped_df
as_flextable(
x,
groups_to = c("titles", "merged", "asis"),
groups_pos = c("left", "asis"),
groups_arrange = NULL,
...
)
# S3 method for data.frame
as_flextable(x, col_keys = names(x), ...)
object to be transformed as flextable
One of titles
, merged
, or asis
.
See examples and vignette("group-rows")
for the result.
When groups_to = "merged"
, grouping columns are reordered according to
group_pos
. Choices are left
(default) or asis
.
TRUE
automatically arranges grouping columns by dplyr::arrange()
.
Specify FALSE
to keep the arrangement of the input data frame.
The default value is NULL
which implies FALSE
to keep the backward
compatibility, but will be TRUE
in the future.
arguments for custom methods
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.gam()
,
as_flextable.glm()
,
as_flextable.grouped_data()
,
as_flextable.htest()
,
as_flextable.lm()
,
as_flextable.summarizor()
,
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")
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`
#> header has 1 row(s)
#> body has 9 row(s)
#> original dataset sample:
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1 setosa NA NA NA NA
#> 2 <NA> 5.1 3.5 1.4 0.2
#> 3 <NA> 4.9 3.0 1.4 0.2
#> 4 versicolor NA NA NA NA
#> 5 <NA> 7.0 3.2 4.7 1.4
as_flextable(grouped_df, groups_to = "titles", hide_grouplabel = TRUE)
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`
#> header has 1 row(s)
#> body has 9 row(s)
#> original dataset sample:
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1 setosa NA NA NA NA
#> 2 <NA> 5.1 3.5 1.4 0.2
#> 3 <NA> 4.9 3.0 1.4 0.2
#> 4 versicolor NA NA NA NA
#> 5 <NA> 7.0 3.2 4.7 1.4
as_flextable(grouped_df, groups_to = "merged")
#> a flextable object.
#> col_keys: `Species`, `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`
#> header has 1 row(s)
#> body has 6 row(s)
#> original dataset sample:
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1 setosa 5.1 3.5 1.4 0.2
#> 2 setosa 4.9 3.0 1.4 0.2
#> 3 versicolor 7.0 3.2 4.7 1.4
#> 4 versicolor 6.4 3.2 4.5 1.5
#> 5 virginica 6.3 3.3 6.0 2.5
as_flextable(grouped_df, groups_to = "asis")
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
#> header has 1 row(s)
#> body has 6 row(s)
#> original dataset sample:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 7.0 3.2 4.7 1.4 versicolor
#> 4 6.4 3.2 4.5 1.5 versicolor
#> 5 6.3 3.3 6.0 2.5 virginica
# For data.frame
iris %>%
head() %>%
as_flextable()
#> a flextable object.
#> col_keys: `Sepal.Length`, `Sepal.Width`, `Petal.Length`, `Petal.Width`, `Species`
#> header has 1 row(s)
#> body has 6 row(s)
#> original dataset sample:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa