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), ...)

Arguments

x

object to be transformed as flextable

groups_to

One of titles, merged, or asis. See examples and vignette("group-rows") for the result.

groups_pos

When groups_to = "merged", grouping columns are reordered according to group_pos. Choices are left (default) or asis.

groups_arrange

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

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.

Examples


# For grouped_df
grouped_df <- iris %>%
  dplyr::group_by(Species) %>%
  dplyr::slice(1, 2)

as_flextable(grouped_df, groups_to = "titles")

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species: setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

Species: versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

Species: virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

as_flextable(grouped_df, groups_to = "titles", hide_grouplabel = TRUE)

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

as_flextable(grouped_df, groups_to = "merged") #> Warning: ftExtra:::as_flextable.data.frame is deprecated and will be removed in the future release. Consider using flextalbe's implementation by running `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

Species

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

setosa

5.1

3.5

1.4

0.2

4.9

3.0

1.4

0.2

versicolor

7.0

3.2

4.7

1.4

6.4

3.2

4.5

1.5

virginica

6.3

3.3

6.0

2.5

5.8

2.7

5.1

1.9

as_flextable(grouped_df, groups_to = "asis") #> Warning: ftExtra:::as_flextable.data.frame is deprecated and will be removed in the future release. Consider using flextalbe's implementation by running `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

7.0

3.2

4.7

1.4

versicolor

6.4

3.2

4.5

1.5

versicolor

6.3

3.3

6.0

2.5

virginica

5.8

2.7

5.1

1.9

virginica

# For data.frame iris %>% head() %>% as_flextable() #> Warning: ftExtra:::as_flextable.data.frame is deprecated and will be removed in the future release. Consider using flextalbe's implementation by running `.S3method("as_flextable", "data.frame", flextable:::as_flextable.data.frame)`

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.1

3.5

1.4

0.2

setosa

4.9

3.0

1.4

0.2

setosa

4.7

3.2

1.3

0.2

setosa

4.6

3.1

1.5

0.2

setosa

5.0

3.6

1.4

0.2

setosa

5.4

3.9

1.7

0.4

setosa