Skip to contents

ivo_table() lets you easily create a table using pretty fonts and colors. If you want the table with masked values use ivo_table_masked().

Usage

ivo_table(
  df,
  extra_header = TRUE,
  exclude_missing = FALSE,
  missing_string = "(Missing)",
  colsums = FALSE,
  rowsums = FALSE,
  sums_string = "Total",
  caption = NA,
  highlight_cols = NULL,
  highlight_rows = NULL,
  percent_by = NA,
  color = "darkgreen",
  font_name = "Arial",
  bold_cols = NULL,
  long_table = FALSE,
  remove_zero_rows = FALSE
)

Arguments

df

A data frame with 1-4 columns

extra_header

Should the variable name be displayed? Defaults to TRUE.

exclude_missing

Whether to exclude missing values from the table. Defaults to FALSE.

missing_string

A string used to indicate missing values. Defaults to "(Missing)".

colsums

A logical indicating whether the sum of each column should be computed. Defaults to FALSE.

rowsums

A logical indicating whether the sum of each row should be computed. Defaults to FALSE.

sums_string

A string that is printed in the column/row where row/column sums are shown. Defaults to "Total".

caption

An optional string containing a table caption.

highlight_cols

A numeric vector containing the indices of the columns that should be highlighted.

highlight_rows

A numeric vector containing the indices of the rows that should be highlighted.

percent_by

Used to get percentages instead of frequencies. There are three options: "row" to get percentages by row (each row sum is 100 percent), "col" to get percentages by column (each the sum of each row to 100 percent) and "tot" to get percentages out of the total (the sum of all cells is 100 percent). The default, NA, means that frequencies are displayed instead.

color

A named color or a color HEX code, used for the lines in the table. Defaults to "darkgreen".

font_name

The name of the font to be used in the table. Defaults to "Arial".

bold_cols

A numeric vector containing the indices of the columns that should use a bold font.

long_table

For one-way tables: FALSE (the default) means that the table will be wide and consist of a single row, TRUE means that the table will be long and consist of a single column.

remove_zero_rows

If set to TRUE, removes all rows that contain nothing but zeros. The default is FALSE.

Value

A stylized flextable.

Details

The functions ivo_table() and ivo_table_masked() takes a data.frame with 1-4 columns. The order of the columns in the data.frame will determine where they will be displayed in the table. The first column will always be displayed at the top of the table. If there are more than one column the following 2-4 columns will be displayed to the left in the order 2, 3, 4. To change how the columns are displayed in the table; change the place of the columns in the data.frame using dplyr::select().

See also

ivo_table_add_mask

Author

Måns Thulin and Kajsa Grind

Examples

# Generate example data
example_data <- data.frame(Year = sample(2020:2023, 50, replace = TRUE),
A = sample(c("Type 1", "Type 2"), 50, replace = TRUE),
B = sample(c("Apples", "Oranges", "Bananas"), 50, replace = TRUE),
C = sample(c("Swedish", "Norwegian", "Chilean"), 50, replace = TRUE))

### 1 way tables ###
data1 <- example_data |> dplyr::select(Year)

ivo_table(data1)

Year

2020

2021

2022

2023

11

7

15

17

ivo_table(data1, extra_header = FALSE) # Remove the header

2020

2021

2022

2023

11

7

15

17

ivo_table(data1, color = "orange") # Change color on table lines

Year

2020

2021

2022

2023

11

7

15

17

ivo_table(data1, long_table = TRUE) # Draw the table in a long format

Year

Count

2020

11

2021

7

2022

15

2023

17

ivo_table(data1, font_name = "Garamond") # Use a different font

Year

2020

2021

2022

2023

11

7

15

17

ivo_table_masked(data1) # No masking because all counts are >=5

Year

2020

2021

2022

2023

11

7

15

17

ivo_table_masked(data1, cell = 15) # Counts below <=15 are masked

Year

2020

2021

2022

2023

1-15

1-15

1-15

17

# With pipes example_data |> dplyr::select(Year) |> ivo_table()

Year

2020

2021

2022

2023

11

7

15

17

### 2-way tables ### data2 <- example_data |> dplyr::select(A, B) data2_swap <- example_data |> dplyr::select(B, A) # Basic tables: ivo_table(data2)

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

ivo_table(data2_swap) # Swap order of the columns

B

A

Apples

Bananas

Oranges

Type 1

6

5

8

Type 2

10

11

10

ivo_table(data2, colsums = TRUE) # Add the sum of each column

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

Total

19

31

ivo_table(data2, rowsums = TRUE) # Add the sum of each row

A

B

Type 1

Type 2

Total

Apples

6

10

16

Bananas

5

11

16

Oranges

8

10

18

ivo_table(data2, caption = "Awesome table") # Add a caption
Awesome table

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

ivo_table(data2, highlight_cols = 3) # Highlight column 3

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

ivo_table(data2, highlight_rows = 2, highlight_cols = 3) # Highlight cell at row 2 column 3

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

ivo_table(data2, bold_cols = 1) # Make the names in the first column bold

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

# Tables with percentages: ivo_table(data2, percent_by = "row") # By row

A

B

Type 1

Type 2

Apples

37,5 %

62,5 %

Bananas

31,2 %

68,8 %

Oranges

44,4 %

55,6 %

ivo_table(data2, percent_by = "col") # By column

A

B

Type 1

Type 2

Apples

31,6 %

32,3 %

Bananas

26,3 %

35,5 %

Oranges

42,1 %

32,3 %

ivo_table(data2, percent_by = "tot") # By total

A

B

Type 1

Type 2

Apples

12,0 %

20,0 %

Bananas

10,0 %

22,0 %

Oranges

16,0 %

20,0 %

# Masked tables: ivo_table_masked(data2)

A

B

Type 1

Type 2

Apples

6

10

Bananas

1-5

11

Oranges

8

10

ivo_table_masked(data2, cell = 7) # Counts <= 7 are masked

A

B

Type 1

Type 2

Apples

1-7

10

Bananas

1-7

11

Oranges

8

10

# Row and column sums are also masked: ivo_table_masked( data2, cell = 3, colsums = TRUE, rowsums = TRUE)

A

B

Type 1

Type 2

Total

Apples

6

10

16

Bananas

5

11

16

Oranges

8

10

18

Total

19

31

50

# Add a note at the end of the table: # (colwidths must be set to the number of columns in the table) ivo_table(data2) |> flextable::add_footer_row(values = "This is a footnote.", colwidths = 3)

A

B

Type 1

Type 2

Apples

6

10

Bananas

5

11

Oranges

8

10

This is a footnote.

# Add footnotes to cells in the table: ivo_table(data2) |> flextable::footnote(i = c(1, 3), j = c(1, 2), value = flextable::as_paragraph(c( "Some remark.", "Some comment.")), ref_symbols = c("a", "b"))

A

B

Type 1

Type 2

Applesa

6

10

Bananas

5

11

Oranges

8b

10

aSome remark.

bSome comment.

# Add footnotes to cells in the table header: ivo_table(data2) |> flextable::footnote(i = 2, j = c(1, 3), value = flextable::as_paragraph(c( "Some remark.", "Some comment.")), ref_symbols = c("a", "b"), part = "header")

A

Ba

Type 1

Type 2b

Apples

6

10

Bananas

5

11

Oranges

8

10

aSome remark.

bSome comment.

### 3-way tables ### data3 <- example_data |> dplyr::select(C, B, Year) ivo_table(data3)

C

B

Year

Chilean

Norwegian

Swedish

Apples

2020

0

4

0

2021

1

0

1

2022

4

1

0

2023

1

2

2

Bananas

2020

0

2

1

2021

1

2

0

2022

0

2

3

2023

1

2

2

Oranges

2020

2

0

2

2021

0

2

0

2022

1

2

2

2023

2

4

1

ivo_table(data3, colsums = TRUE, rowsums = TRUE) # Add the sum of each column and each row

C

B

Year

Chilean

Norwegian

Swedish

Total

Apples

2020

0

4

0

4

2021

1

0

1

2

2022

4

1

0

5

2023

1

2

2

5

Bananas

2020

0

2

1

3

2021

1

2

0

3

2022

0

2

3

5

2023

1

2

2

5

Oranges

2020

2

0

2

4

2021

0

2

0

2

2022

1

2

2

5

2023

2

4

1

7

Total

13

23

14

50

ivo_table_masked( data3, cell = 3, caption = "Values between 1 and 3 are masked." )
Values between 1 and 3 are masked.

C

B

Year

Chilean

Norwegian

Swedish

Apples

2020

0

4

0

2021

1-3

0

1-3

2022

4

1-3

0

2023

1-3

1-3

1-3

Bananas

2020

0

1-3

1-3

2021

1-3

1-3

0

2022

0

1-3

1-3

2023

1-3

1-3

1-3

Oranges

2020

1-3

0

1-3

2021

0

1-3

0

2022

1-3

1-3

1-3

2023

1-3

4

1-3

### 4-way tables ### data4 <- example_data |> dplyr::select(Year, B, C, A) ivo_table(data4)

Year

B

C

A

2020

2021

2022

2023

Apples

Chilean

Type 1

0

0

2

0

Type 2

0

1

2

1

Norwegian

Type 1

1

0

0

1

Type 2

3

0

1

1

Swedish

Type 1

0

1

0

1

Type 2

0

0

0

1

Bananas

Chilean

Type 1

0

1

0

0

Type 2

0

0

0

1

Norwegian

Type 1

1

1

0

1

Type 2

1

1

2

1

Swedish

Type 1

1

0

0

0

Type 2

0

0

3

2

Oranges

Chilean

Type 1

1

0

0

0

Type 2

1

0

1

2

Norwegian

Type 1

0

1

1

3

Type 2

0

1

1

1

Swedish

Type 1

1

0

1

0

Type 2

1

0

1

1

ivo_table(data4, remove_zero_rows = TRUE) # Remove the row with zeros

Year

B

C

A

2020

2021

2022

2023

Apples

Chilean

Type 1

0

0

2

0

Type 2

0

1

2

1

Norwegian

Type 1

1

0

0

1

Type 2

3

0

1

1

Swedish

Type 1

0

1

0

1

Type 2

0

0

0

1

Bananas

Chilean

Type 1

0

1

0

0

Type 2

0

0

0

1

Norwegian

Type 1

1

1

0

1

Type 2

1

1

2

1

Swedish

Type 1

1

0

0

0

Type 2

0

0

3

2

Oranges

Chilean

Type 1

1

0

0

0

Type 2

1

0

1

2

Norwegian

Type 1

0

1

1

3

Type 2

0

1

1

1

Swedish

Type 1

1

0

1

0

Type 2

1

0

1

1

# Add the sum of each column and each row and highlight column 6: ivo_table( data4, colsums = TRUE, rowsums = TRUE, highlight_cols = 6)

Year

B

C

A

2020

2021

2022

2023

Total

Apples

Chilean

Type 1

0

0

2

0

2

Type 2

0

1

2

1

4

Norwegian

Type 1

1

0

0

1

2

Type 2

3

0

1

1

5

Swedish

Type 1

0

1

0

1

2

Type 2

0

0

0

1

1

Bananas

Chilean

Type 1

0

1

0

0

1

Type 2

0

0

0

1

1

Norwegian

Type 1

1

1

0

1

3

Type 2

1

1

2

1

5

Swedish

Type 1

1

0

0

0

1

Type 2

0

0

3

2

5

Oranges

Chilean

Type 1

1

0

0

0

1

Type 2

1

0

1

2

4

Norwegian

Type 1

0

1

1

3

5

Type 2

0

1

1

1

3

Swedish

Type 1

1

0

1

0

2

Type 2

1

0

1

1

3

Total

11

7

15

17

50

ivo_table_masked(data4, colsums = TRUE, rowsums = TRUE)

Year

B

C

A

2020

2021

2022

2023

Total

Apples

Chilean

Type 1

0

0

1-5

0

NA

Type 2

0

1-5

1-5

1-5

NA

Norwegian

Type 1

1-5

0

0

1-5

NA

Type 2

1-5

0

1-5

1-5

NA

Swedish

Type 1

0

1-5

0

1-5

NA

Type 2

0

0

0

1-5

NA

Bananas

Chilean

Type 1

0

1-5

0

0

NA

Type 2

0

0

0

1-5

NA

Norwegian

Type 1

1-5

1-5

0

1-5

NA

Type 2

1-5

1-5

1-5

1-5

NA

Swedish

Type 1

1-5

0

0

0

NA

Type 2

0

0

1-5

1-5

NA

Oranges

Chilean

Type 1

1-5

0

0

0

NA

Type 2

1-5

0

1-5

1-5

NA

Norwegian

Type 1

0

1-5

1-5

1-5

NA

Type 2

0

1-5

1-5

1-5

NA

Swedish

Type 1

1-5

0

1-5

0

NA

Type 2

1-5

0

1-5

1-5

NA

Total

NA

NA

NA

NA

NA