Skip to contents

Turns a data frame into a table styled with OMNI Institute's fonts, colours and row striping. The result is a flextable object, so it can be piped into any flextable function for further customisation (for example to set column widths, merge cells, or format specific rows).

Usage

omni_table(
  df,
  group_by = NULL,
  first_col_gray = FALSE,
  caption = NULL,
  with_stripes = TRUE,
  dark_group_rows = FALSE
)

Arguments

df

The data frame to turn into a table.

group_by

Optional character vector of grouping variable(s). When supplied, the data is grouped and the group-label rows are shaded. Defaults to NULL (no grouping).

first_col_gray

Should the first column be shaded gray (with white text)? Defaults to FALSE.

caption

The table caption. Defaults to NULL (no caption).

with_stripes

Should rows use a striped (zebra) pattern? Defaults to TRUE.

dark_group_rows

When group_by is supplied, should the group rows use a darker navy shade instead of steel blue? Defaults to FALSE.

Value

A flextable object (of class omni_table).

Details

By default the table uses an autofit layout and fills the width of the page or container, sizing each column to fit its content.

When rendered in the OMNI PDF report, column widths are held constant where a table breaks across pages, and the header row is repeated at the top of each page the table spans. This is handled automatically and needs no extra code.

To control the relative width of columns, pipe the result through width and switch the layout to "fixed" with set_table_properties. The widths act as proportions and are scaled to fill the available width. There must be one width per column:


omni_table(df) |>
  flextable::width(width = c(2.5, 1, 1, 1)) |>
  flextable::set_table_properties(layout = "fixed", width = 1)

See also

width and set_table_properties for adjusting column widths and the table layout.

Examples

# Basic table
palmerpenguins::penguins |>
  dplyr::slice(1:3) |>
  omni_table()

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2,007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2,007

# Shade the first column palmerpenguins::penguins |> dplyr::slice(1:3) |> omni_table(first_col_gray = TRUE)

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2,007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2,007

# Group rows by a variable palmerpenguins::penguins |> dplyr::slice(1:3, .by = species) |> omni_table(group_by = "species")

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Torgersen

39.5

17.4

186

3,800

female

2,007

Torgersen

40.3

18.0

195

3,250

female

2,007

Gentoo

Biscoe

46.1

13.2

211

4,500

female

2,007

Biscoe

50.0

16.3

230

5,700

male

2,007

Biscoe

48.7

14.1

210

4,450

female

2,007

Chinstrap

Dream

46.5

17.9

192

3,500

female

2,007

Dream

50.0

19.5

196

3,900

male

2,007

Dream

51.3

19.2

193

3,650

male

2,007

# Add a caption palmerpenguins::penguins |> dplyr::slice(1:3) |> omni_table(caption = "Table 1. [Insert Table Name]")
Table 1. [Insert Table Name]

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2,007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2,007

# Without the striped pattern palmerpenguins::penguins |> dplyr::slice(1:3) |> omni_table(with_stripes = FALSE)

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2,007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2,007

# Overwrite number formatting by transforming to character first palmerpenguins::penguins |> dplyr::slice(1:3) |> dplyr::mutate(year = as.character(year)) |> omni_table()

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2007

# Set column widths manually. Pipe through flextable::width() and switch # the layout to "fixed" so the widths are respected. The width vector needs # one entry per column (palmerpenguins::penguins has 8); here the first # column is made wider than the rest. palmerpenguins::penguins |> dplyr::slice(1:3) |> omni_table() |> flextable::width(width = c(2, 1, 1, 1, 1, 1, 1, 1)) |> flextable::set_table_properties(layout = "fixed", width = 1)

species

island

bill_length_mm

bill_depth_mm

flipper_length_mm

body_mass_g

sex

year

Adelie

Torgersen

39.1

18.7

181

3,750

male

2,007

Adelie

Torgersen

39.5

17.4

186

3,800

female

2,007

Adelie

Torgersen

40.3

18.0

195

3,250

female

2,007