flextable
flextable copied to clipboard
PDF - repeated headers overlap the content of merged cell
I have been trying to use fit_to_width() to produce tables with Quarto that fit a specific width within the page of the PDF. Unfortunately, autofit() has not helped, and fit_to_width() has not worked as intended. Whenever I use it, it replaces the table content with some placeholder text.
Please find the code and the issue below:
mpd.CFI.CHet.bS.gam.model_summaries <- structure(list(Scale = c("Global", "Global", "Global", "Global",
"Global", "Global", "Global", "Global", "Hemispheric", "Hemispheric",
"Hemispheric", "Hemispheric", "Hemispheric", "Hemispheric", "Hemispheric",
"Hemispheric", "Realm", "Realm", "Realm", "Realm", "Realm", "Realm",
"Realm", "Realm", "Biome", "Biome", "Biome", "Biome", "Biome",
"Biome", "Biome", "Biome"), Taxon = c("Amphibians", "Amphibians",
"Birds", "Birds", "Mammals", "Mammals", "Squamates", "Squamates",
"Amphibians", "Amphibians", "Birds", "Birds", "Mammals", "Mammals",
"Squamates", "Squamates", "Amphibians", "Amphibians", "Birds",
"Birds", "Mammals", "Mammals", "Squamates", "Squamates", "Amphibians",
"Amphibians", "Birds", "Birds", "Mammals", "Mammals", "Squamates",
"Squamates"), Response = c("MPD", "MPD", "MPD", "MPD", "MPD",
"MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD",
"MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD",
"MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD", "MPD"
), Predictors = c("s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)",
"s(Clim. Freq.) + s(Clim. Het.)", "s(Clim. Freq.) + s(Clim. Het.)"
), Smooth.basis = c("Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines",
"Thin-plate splines", "Thin-plate splines", "Thin-plate splines"
), Adj..R.2 = c(0.175, 0.175, 0.077, 0.077, 0.053, 0.053, 0.295,
0.295, 0.098, 0.098, 0.079, 0.079, 0.045, 0.045, 0.154, 0.154,
0.037, 0.037, 0.076, 0.076, 0.065, 0.065, 0.148, 0.148, 0.132,
0.132, 0.074, 0.074, 0.016, 0.016, 0.089, 0.089), dev.uniq.non.linear = c(0.039,
0.039, 0.038, 0.038, 0.007, 0.007, 0.017, 0.017, 0.026, 0.026,
0.031, 0.031, 0.007, 0.007, 0.036, 0.036, 0.025, 0.025, 0.018,
0.018, 0.017, 0.017, 0.035, 0.035, 0.027, 0.027, 0.008, 0.008,
0.015, 0.015, 0.031, 0.031), Predictor = c("s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)", "s(CFI.bS)", "s(heterogeneity.meanEuclid)",
"s(CFI.bS)", "s(heterogeneity.meanEuclid)"), edf = c(8.94725336461062,
8.24474929940239, 8.94402808538318, 8.0433126490782, 8.5972718307536,
6.90985590548356, 8.96129511999248, 8.5986067320282, 8.96563886692967,
8.30961147988176, 8.98433079686555, 8.23076432847517, 8.90263880010934,
6.86404424849931, 8.97398220224686, 8.69075105939943, 8.95949377680743,
8.43700193247956, 8.97097220465126, 8.17551845665404, 8.94535035010068,
6.58312089445243, 8.97836907118496, 8.72640907751301, 8.8820397256442,
8.38989735222463, 8.92777933838305, 8.17965037889696, 8.44882219612362,
6.67862792516023, 8.94350263716568, 8.72142016558614), Ref.df = c(8.99918106400064,
8.84762420890626, 8.99907385783694, 8.76437213410865, 8.95428497695193,
8.02973917476159, 8.99955633527575, 8.95532361379465, 8.99964476828757,
8.87176481676359, 8.9999260146117, 8.84376427000312, 8.9971782037767,
7.99158455565594, 8.99979564755562, 8.97315094545215, 8.99951059845159,
8.91317490928138, 8.99974645533773, 8.82184062675737, 8.9991041994842,
7.7535736056498, 8.99985929333262, 8.97888578568561, 8.99581098178945,
8.89871654216935, 8.99841268199284, 8.8235135826169, 8.91354725246866,
7.8363113129187, 8.99902711654054, 8.97812146878775), F = c(4036.31513633459,
69.7911843367783, 1759.88065677894, 262.705849512569, 1443.42921525763,
194.454421113521, 8955.41928264452, 92.0348721761402, 2002.68924477316,
74.588383290832, 1780.02960506767, 322.540064864731, 1165.52780160569,
200.429583280523, 3622.23334399431, 223.319132967035, 622.826313416701,
123.844805888992, 1695.43875878549, 301.77693780504, 1757.59584121885,
203.782496099787, 3445.81563957982, 222.04847289181, 2850.33857052798,
108.262471571175, 1616.33541454028, 449.583131620976, 343.437567443098,
98.3637516856449, 1751.09643636829, 340.923735307398), p.value = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = "data.frame", row.names = c(NA,
-32L))
#| label: tbl-mpd-cfi-clim-het-allscales
#| tbl-cap: "**Parameter estimates and model summaries from thin-plate regression smooth splines for the effects of climatic frequency and climatic heterogeneity on mean phylogenetic distances of tetrapod communities.** Climatic frequency was measured across four geographical scales: global, east-west hemisphere, biogeographical realm, and biome extents. Mean phylogenetic distances denote how closely-related co-occurring taxa are in a given community, and was estimate for each tetrapod group separately, being them: amphibians, birds, mammals, and squamate reptiles. Effective estimated degrees of freedom (edf) are taken as number of data minus model degrees of freedom (see Methods)."
#| echo: false
#| message: false
#| warning: false
mpd.CFI.CHet.bS.gam.model_summaries %>%
mutate(p.value = ifelse(p.value <= 0.0001, 0.001, 0)) %>%
group_by(Scale, Taxon, Predictor) %>%
mutate("signif" = NA) %>%
dplyr::select(
-c("Response",
"Predictors",
"Smooth.basis")
) %>%
flextable() %>%
merge_v(1:4) %>%
valign(valign = 'top') %>%
add_header_row(values = "Thin-plate regression splines with formula: \nMPD ~ s(Clim. Freq.) + s(Clim. Het.)",
colwidths = 10,
top = T) %>%
bold(~ `p.value` <= 0.001, "p.value") %>%
theme_vanilla() %>%
mk_par(j = "signif", value = as_paragraph(pvalue_format(p.value)) ) %>%
colformat_double(j = c("edf",
"Ref.df",
"F" ,
"dev.uniq.non.linear"), digits = 3) %>%
set_header_labels(Scale = "Geographical Scale",
Adj..R.2 = "Total Adj. R2",
dev.uniq.non.linear = "Non-linear Adj. R2",
edf = "edf",
Ref.df = "df",
p.value = "p-value",
signif = "") %>%
align(j = "signif", align = "left") %>%
padding(padding.right = 0, j = "p.value", part = "all") %>%
bold(j = "signif", bold = TRUE) %>%
# padding(padding.left = 0, j = "signif", part = "all") %>%
# set_table_properties(width = 1, layout = "autofit") %>%
add_footer_lines(values = c(
"Signif. codes: 0 <= '***' < 0.001 < '**' < 0.01 < '*' < 0.05 < '.' < 0.1 < '' < 1" )) %>%
# flextable::fontsize(size = 9, part = "all") %>%
font(fontname = "Times New Roman", part = "all") %>%
autofit() %>%
fit_to_width(max_width = 7)
Outcome:
> sessionInfo()
R version 4.3.3 RC (2024-02-22 r85999 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 26120)
Matrix products: default
locale:
[1] LC_COLLATE=English_Canada.utf8 LC_CTYPE=English_Canada.utf8 LC_MONETARY=English_Canada.utf8
[4] LC_NUMERIC=C LC_TIME=English_Canada.utf8
time zone: America/Toronto
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] gt_0.11.0.9000 kableExtra_1.4.0 flextable_0.9.7.008 stringr_1.5.1
[5] dplyr_1.1.4 tidyr_1.3.1 data.table_1.15.4
loaded via a namespace (and not attached):
[1] xfun_0.45 remotes_2.5.0 htmlwidgets_1.6.4 devtools_2.4.5
[5] processx_3.8.4 callr_3.7.6 ps_1.7.7 vctrs_0.6.5
[9] tools_4.3.3 generics_0.1.3 curl_5.2.1 parallel_4.3.3
[13] tibble_3.2.1 fansi_1.0.6 pkgconfig_2.0.3 desc_1.4.3
[17] RcppParallel_5.1.7 uuid_1.2-0 lifecycle_1.0.4 compiler_4.3.3
[21] textshaping_0.4.0 munsell_0.5.1 httpuv_1.6.15 fontquiver_0.2.1
[25] fontLiberation_0.1.0 sass_0.4.9 usethis_2.2.3 htmltools_0.5.8.1
[29] urlchecker_1.0.1 later_1.3.2 pillar_1.9.0 crayon_1.5.3
[33] gfonts_0.2.0 ellipsis_0.3.2 openssl_2.2.0 rsconnect_1.3.1
[37] cachem_1.1.0 sessioninfo_1.2.2 mime_0.12 fontBitstreamVera_0.1.1
[41] commonmark_1.9.1 tidyselect_1.2.1 zip_2.3.1 digest_0.6.36
[45] stringi_1.8.4 purrr_1.0.2 fastmap_1.2.0 grid_4.3.3
[49] colorspace_2.1-0 cli_3.6.1 magrittr_2.0.3 Rfast_2.1.0
[53] crul_1.4.2 pkgbuild_1.4.4 utf8_1.2.4 withr_3.0.0
[57] scales_1.3.0 gdtools_0.3.7 promises_1.3.0 RcppZiggurat_0.1.6
[61] rmarkdown_2.27 officer_0.6.6 askpass_1.2.0 ragg_1.3.2
[65] memoise_2.0.1 shiny_1.8.1.1 evaluate_0.24.0 knitr_1.47
[69] viridisLite_0.4.2 miniUI_0.1.1.1 markdown_1.13 profvis_0.3.8
[73] rlang_1.1.4 Rcpp_1.0.12 xtable_1.8-4 glue_1.7.0
[77] httpcode_0.3.0 xml2_1.3.6 pkgload_1.4.0 svglite_2.1.3
[81] rstudioapi_0.16.0 jsonlite_1.8.8 R6_2.5.1 systemfonts_1.1.0
[85] fs_1.6.4