Chapter 8 Making Tables with flextable

NOTES: - j refers to the column - i refers to the row number

8.1 APA Table Components

8.3 Add a Horizontal border (AKA horizontal “spanner”)

hline(., i=4, j=1:2, part = "body")

8.4 Change font and font size

  glm_table<-flextable::font(glm_table, part = "all", fontname = "Times") %>% # Font
                        fontsize(., size = 11, part = "all") # Font size

8.5 Grouped table

cars=rownames_to_column(mtcars, var = "Model")
test=flextable::as_grouped_data(x=cars, groups = c("cyl"))

8.6 Adjust line spacing

flextable::line_spacing(space = "0.5")

8.7 Set global options for all flextables

# set global options for all flextables
flextable::set_flextable_defaults(font.family = "Times New Roman", font.size = 11)

# create an fp_border object to set the borders for the flextables
border.test=officer::fp_border(color = "black", style = "solid", width = 1)

8.8 Complete Example

library(flextable)
## 
## Attaching package: 'flextable'
## The following object is masked from 'package:purrr':
## 
##     compose
# set global options for all flextables
flextable::set_flextable_defaults(font.family = "Times New Roman", font.size = 11)

# create an fp_border object to set the borders for the flextables
border.test=officer::fp_border(color = "black", style = "solid", width = 1)


mtcars |> 
  slice(1:5) |> 
  flextable() |>
  # FIX BORDERS IN THE HEADER
  hline_bottom(border = border.test, part = "header") |>
  hline_top(border = border.test, part = "header") |>
  # CREATE A TITLE HEADER; APPLY FORMATTING
  add_header_lines(values = "Car Stuff") |> 
  hline_top(border = fp_border_default(width = 0), part = "header") |> 
  flextable::align(align = "left", part = "header", i=1) |> 
  italic(part = "header", i=1) |> 
  # FIX BORDER IN TABLE BODY
  hline_bottom(border = border.test, part = "body") |> 
  # Line spacing
  flextable::line_spacing(space = "0.5") |> 
  # Add a footer
  flextable::add_footer_lines(values = "A test footer") |> 
  # SET COLUMN WIDTH/DIMENSIONS
  autofit(part = "all") |> 
  width(j=1, width = 0.4, unit = "in")

8.9 Wraper-function to create APA-style tables

This took way too many hours. You’re welcome.

APA_table=function(flextable_object, table_title, include_note){
  
  
  flextable_object=flextable_object |> 
    hline_bottom(border = border.test, part = "header") |>
    hline_top(border = border.test, part = "header") |>
    # CREATE A TITLE HEADER; APPLY FORMATTING
    add_header_lines(values = table_title) |> 
    hline_top(border = fp_border_default(width = 0), part = "header") |> 
    flextable::align(align = "left", part = "header", i=1) |> 
    italic(part = "header", i=1) |> 
    # FIX BORDER IN TABLE BODY
    hline_bottom(border = border.test, part = "body") |> 
    # SET FONT
    flextable::font(part = "all", fontname = "Times New Roman") |> 
    flextable::fontsize(part = "all", size = 11) |> 
    # SET COLUMN WIDTH/DIMENSIONS
    autofit(part = "all") 
  
  if(include_note==FALSE) (return(flextable_object))
  if(include_note!=FALSE) (flextable_object=add_footer_lines(flextable_object, 
                                                             values = paste0("Note."," ", include_note)) |> 
                             fontsize(part = "footer", size = 11) |>  
                             font(part = "footer", fontname = "Times"))
  
  return(flextable_object)
  
}