knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(varnish)

Example

table_contents <- airquality[1:10,c(6, 1:4)]
table_contents

"Varnish" a table

The varnish() function is a wrapper for several functions, combining several steps of finishing a table:

table_contents %>% 
  flextable::flextable() %>% 
  varnish(headers     = list(c("", "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
          colspan     = list(c(1, 4),
                             NULL),
          underline_j = 2,
          widths      = c(10, 23, 23, 23, 23))

Add column headers

The add_headers() function deletes the existing table header and then adds (multiple) column headers. It is a wrapper for flextable::delete_part() and flextable::add_header_row().

table_contents %>% 
  flextable::flextable() %>% 
  add_headers(headers = list(c(NA, "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
              colspan = list(c(1, 4), NULL))

Apply style to a table

The style_table() function is used to apply an overall design to a flextable. It is recommended to use add_headers() (to add column headers) before applying this function.

table_contents %>% 
  flextable::flextable() %>% 
  add_headers(c("Day", "Ozone", "Solar", "Wind", "Temp")) %>% 
  style_table()

Underline header

table_contents %>% 
  flextable::flextable() %>% 
  add_headers(headers = list(c(NA, "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
              colspan = list(c(1, 4), NULL)) %>% 
  style_table() %>% 
  underline_header(j = 2)

Set widths of all columns

table_contents %>% 
  flextable::flextable() %>% 
  add_headers(headers = list(c(NA, "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
              colspan = list(c(1, 4), NULL)) %>% 
  style_table() %>% 
  underline_header(j = 2) %>% 
  colwidths(c(10, 23, 23, 23, 23))

Add footnotes

table_contents %>% 
  flextable::flextable() %>% 
  add_headers(headers = list(c(NA, "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
              colspan = list(c(1, 4), NULL)) %>% 
  style_table() %>% 
  underline_header(j = 2) %>% 
  add_footnote("Daily air quality measurements in New York, May 1973.")
table_contents %>% 
  flextable::flextable() %>% 
  add_headers(headers = list(c(NA, "Air quality"),
                             c("Day", "Ozone", "Solar", "Wind", "Temp")),
              colspan = list(c(1, 4), NULL)) %>% 
  style_table() %>% 
  underline_header(j = 2) %>% 
  add_footnote("Daily air quality measurements in New York, May 1973.") %>% 
  add_footnote("\u00B0F",
               i = 2, j = 5, part = "header",
               symb = marker(1))


neilstats/varnish documentation built on Sept. 22, 2022, 7:28 a.m.