flextable selectors

Why selectors

Selectors can be used to specify the rows and columns where an operation should happened.

Many flextable functions have selectors i and j: bg, bold, border, color, padding, fontsize, italic, align, compose, …

It makes conditional formatting very easy. As the underlying datasets (for body but also header and footer parts) are available, selectors can easily be used and operations can be seamlessly piped (with magrittr::%>%).

qflextable(head(iris)) %>% 
  color(~ Sepal.Length < 5, color = "orange", ~ Sepal.Width + Petal.Length ) %>% 
  color(~ Sepal.Length > 4.99, ~ Sepal.Length, color = "red")

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

Default values for i and j are NULL, NULL value is interpreted as all rows or all columns.

Usage

i for rows selection and j for columns selection can be expressed in different ways:

as a formula

Use i = ~ Species %in% "versicolor" to select all rows where values from column ‘Species’ are “versicolor”.

The argument expression is ~ and then an R expression. There is no need to quote variable, when the formula will be evaluated, values from the corresponding dataset (to the part) will be used.

To express multiple conditions, use operator & or |:

i = ~ Sepal.Length < 5 & Species %in% "versicolor"

The columns can be selected with a formula also. Use operator + for multiple columns. To select columns Species and Sepal.Length: j = ~ Species + Sepal.Length

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

as a character vector

Argument j supports simple character vector containing the col_key names.

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

as a integer vector

Each element is the row number or col_key number:

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

as a logical vector

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

Selectors and flextable parts

Several operations (bold, color, padding, compose) accept part="all". In this case all mean to apply to each part of the flextable (header, body and footer if any). That’s useful for many cases:

border <- officer::fp_border()
ft <- flextable(head(iris, n = 10))
ft <- vline(ft, j = c('Sepal.Length', 'Sepal.Width'), border = border, part = "all")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

ft <- color(ft, i = 1, color = "red", part = "header")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

ft <- color(ft, i = ~ Sepal.Length < 5, 
               j = c('Petal.Length', 'Petal.Width'), 
               color = "red", part = "body")
ft

Sepal.Length

Sepal.Width

Petal.Length

Petal.Width

Species

5.100

3.500

1.400

0.200

setosa

4.900

3.000

1.400

0.200

setosa

4.700

3.200

1.300

0.200

setosa

4.600

3.100

1.500

0.200

setosa

5.000

3.600

1.400

0.200

setosa

5.400

3.900

1.700

0.400

setosa

4.600

3.400

1.400

0.300

setosa

5.000

3.400

1.500

0.200

setosa

4.400

2.900

1.400

0.200

setosa

4.900

3.100

1.500

0.100

setosa

The most efficient selector for rows is the formula expression.

The most efficient selector for columns is the character vector.

Selectors expressed as formula connot be used everywhere: