Chapter 8 Making Tables with flextable
NOTES: - j refers to the column - i refers to the row number
8.2 Indent values
https://davidgohel.github.io/flextable/reference/padding.html https://stackoverflow.com/questions/64134725/indentation-in-the-first-column-of-a-flextable-object
Use the padding
function:
<- padding(ft, i=2, j=1, padding.left=20) ft
8.4 Change font and font size
<-flextable::font(glm_table, part = "all", fontname = "Times") %>% # Font
glm_tablefontsize(., size = 11, part = "all") # Font size
8.5 Grouped table
=rownames_to_column(mtcars, var = "Model")
cars=flextable::as_grouped_data(x=cars, groups = c("cyl")) test
8.7 Set global options for all flextables
# set global options for all flextables
::set_flextable_defaults(font.family = "Times New Roman", font.size = 11)
flextable
# create an fp_border object to set the borders for the flextables
=officer::fp_border(color = "black", style = "solid", width = 1) border.test
8.8 Complete Example
library(flextable)
##
## Attaching package: 'flextable'
## The following object is masked from 'package:purrr':
##
## compose
# set global options for all flextables
::set_flextable_defaults(font.family = "Times New Roman", font.size = 11)
flextable
# create an fp_border object to set the borders for the flextables
=officer::fp_border(color = "black", style = "solid", width = 1)
border.test
|>
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") |>
::align(align = "left", part = "header", i=1) |>
flextableitalic(part = "header", i=1) |>
# FIX BORDER IN TABLE BODY
hline_bottom(border = border.test, part = "body") |>
# Line spacing
::line_spacing(space = "0.5") |>
flextable# Add a footer
::add_footer_lines(values = "A test footer") |>
flextable# SET COLUMN WIDTH/DIMENSIONS
autofit(part = "all") |>
width(j=1, width = 0.4, unit = "in")
Car Stuff | ||||||||||
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
A test footer |
8.9 Wraper-function to create APA-style tables
This took way too many hours. You’re welcome.
=function(flextable_object, table_title, include_note){
APA_table
=flextable_object |>
flextable_objecthline_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") |>
::align(align = "left", part = "header", i=1) |>
flextableitalic(part = "header", i=1) |>
# FIX BORDER IN TABLE BODY
hline_bottom(border = border.test, part = "body") |>
# SET FONT
::font(part = "all", fontname = "Times New Roman") |>
flextable::fontsize(part = "all", size = 11) |>
flextable# 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)
}