Skip to contents

Produce a flextable from a 'tabular' object produced with function tables::tabular().

When as_flextable.tabular=TRUE, the first column is used as row separator acting as a row title. It can be formated with arguments fp_p (the formatting properties of the paragraph) and row_title that specifies the content and eventually formattings of the content.

Two hidden columns can be used for conditional formatting after the creation of the flextable (use only when spread_first_col=TRUE):

  • The column .row_title that contains the title label

  • The column .type that can contain the following values:

    • "one_row": Indicates that there is only one row for this group. In this case, the row is not expanded with a title above.

    • "list_title": Indicates a row that serves as a title for the data that are displayed after it.

    • "list_data": Indicates rows that follow a title and contain data to be displayed.

The result is paginated (see paginate()).

Usage

# S3 method for tabular
as_flextable(
  x,
  spread_first_col = FALSE,
  fp_p = fp_par(text.align = "center", padding.top = 4),
  row_title = as_paragraph(as_chunk(.row_title)),
  add_tab = FALSE,
  ...
)

Arguments

x

object produced by tables::tabular().

spread_first_col

if TRUE, first row is spread as a new line separator instead of being a column. This helps to reduce the width and allows for clear divisions.

fp_p

paragraph formatting properties associated with row titles, see fp_par().

row_title

a call to as_paragraph() - it will be applied to the row titles if any when spread_first_col=TRUE.

add_tab

adds a tab in front of "list_data" label lines (located in column .type).

...

unused argument

Examples

if (require("tables")) {
  set.seed(42)
  genders <- c("Male", "Female")
  status <- c("low", "medium", "high")
  Sex <- factor(sample(genders, 100, rep = TRUE))
  Status <- factor(sample(status, 100, rep = TRUE))
  z <- rnorm(100) + 5
  fmt <- function(x) {
    s <- format(x, digits = 2)
    even <- ((1:length(s)) %% 2) == 0
    s[even] <- sprintf("(%s)", s[even])
    s
  }
  tab <- tabular(
    Justify(c) * Heading() * z *
      Sex * Heading(Statistic) *
      Format(fmt()) *
      (mean + sd) ~ Status
  )
  as_flextable(tab)
}
#> Loading required package: tables

Sex

Statistic

Status

high

low

medium

Female

mean

4.57

4.73

5.09

sd

(1.04)

(0.92)

(0.66)

Male

mean

4.58

4.40

5.64

sd

(0.83)

(0.82)

(1.11)

if (require("tables")) { tab <- tabular( (Species + 1) ~ (n = 1) + Format(digits = 2) * (Sepal.Length + Sepal.Width) * (mean + sd), data = iris ) as_flextable(tab) }

Species

Sepal.Length

Sepal.Width

n

mean

sd

mean

sd

setosa

50

5.01

0.35

3.43

0.38

versicolor

50

5.94

0.52

2.77

0.31

virginica

50

6.59

0.64

2.97

0.32

All

150

5.84

0.83

3.06

0.44

if (require("tables")) { x <- tabular((Factor(gear, "Gears") + 1) * ((n = 1) + Percent() + (RowPct = Percent("row")) + (ColPct = Percent("col"))) ~ (Factor(carb, "Carburetors") + 1) * Format(digits = 1), data = mtcars) ft <- as_flextable( x, spread_first_col = TRUE, row_title = as_paragraph( colorize("Gears: ", color = "#666666"), colorize(as_b(.row_title), color = "red") ) ) ft }

Carburetors

1

2

3

4

6

8

All

Gears: 3

n

3

4

3

5

0

0

15

Percent

9

12

9

16

0

0

47

RowPct

20

27

20

33

0

0

100

ColPct

43

40

100

50

0

0

47

Gears: 4

n

4

4

0

4

0

0

12

Percent

12

12

0

12

0

0

38

RowPct

33

33

0

33

0

0

100

ColPct

57

40

0

40

0

0

38

Gears: 5

n

0

2

0

1

1

1

5

Percent

0

6

0

3

3

3

16

RowPct

0

40

0

20

20

20

100

ColPct

0

20

0

10

100

100

16

Gears: All

n

7

10

3

10

1

1

32

Percent

22

31

9

31

3

3

100

RowPct

22

31

9

31

3

3

100

ColPct

100

100

100

100

100

100

100

if (require("tables")) { tab <- tabular( (mean + mean) * (Sepal.Length + Sepal.Width) ~ 1, data = iris ) as_flextable(tab) }

All

mean

Sepal.Length

5.843

Sepal.Width

3.057

mean

Sepal.Length

5.843

Sepal.Width

3.057