Learn R Programming

flextable (version 0.9.11)

align: Set text alignment

Description

Change the text alignment of selected rows and columns of a flextable.

Usage

align(
  x,
  i = NULL,
  j = NULL,
  align = "left",
  part = c("body", "header", "footer", "all")
)

align_text_col(x, align = "left", header = TRUE, footer = TRUE)

align_nottext_col(x, align = "right", header = TRUE, footer = TRUE)

Arguments

x

a 'flextable' object, see flextable-package to learn how to create 'flextable' object.

i

row selector, see section Row selection with the i parameter in <Selectors in flextable>.

j

column selector, see section Column selection with the j parameter in <Selectors in flextable>.

align

text alignment - a single character value, or a vector of character values equal in length to the number of columns selected by j. Expected values must be from the set ('left', 'right', 'center', or 'justify').

If the number of columns is a multiple of the length of the align parameter, then the values in align will be recycled across the remaining columns.

part

part selector, see section Part selection with the part parameter in <Selectors in flextable>. Value 'all' can be used.

header

should the header be aligned with the body

footer

should the footer be aligned with the body

See Also

Other sugar functions for table style: bg(), bold(), color(), empty_blanks(), font(), fontsize(), highlight(), italic(), keep_with_next(), line_spacing(), padding(), rotate(), style(), tab_settings(), valign()

Examples

Run this code
# Table of 6 columns
ft_car <- flextable(head(mtcars)[, 2:7])

# All 6 columns right aligned
align(ft_car, align = "right", part = "all")

# Manually specify alignment of each column
align(
  ft_car,
  align = c("left", "right", "left", "center", "center", "right"),
  part = "all"
)

# Center-align column 2 and left-align column 5
align(ft_car, j = c(2, 5), align = c("center", "left"), part = "all")

# Alternate left and center alignment across columns 1-4 for header only
align(ft_car, j = 1:4, align = c("left", "center"), part = "header")
ftab <- flextable(mtcars)
ftab <- align_text_col(ftab, align = "left")
ftab <- align_nottext_col(ftab, align = "right")
ftab

Run the code above in your browser using DataLab