The gt package follows a grammar of tables approach, similar to how ggplot2 implements the grammar of graphics. Every table has distinct components that can be styled and modified independently.
# First install webshot2 if needed# install.packages("webshot2")# Save as PNGclinical_table %>%gtsave("clinical_results_table.png", expand =10)# Save as PDFclinical_table %>%gtsave("clinical_results_table.pdf")
Note
Saving tables as images requires the webshot2 package. Install it with install.packages("webshot2") if you haven’t already.
HTML output
For web publishing and interactive documents:
# Save as standalone HTMLclinical_table %>%gtsave("clinical_results_table.html")# Or get the HTML codehtml_code <- clinical_table %>%as_raw_html()
Word and LaTeX
For journal submissions:
# Export to RTF (which can be opened in Word)clinical_table %>%gtsave("clinical_results.rtf")# For LaTeXlatex_code <- clinical_table %>%as_latex()# Save to filewriteLines(latex_code, "clinical_results.tex")
Tip
For Word documents, the RTF format provides the best compatibility. Most word processors can open RTF files while preserving table formatting.
Best practices
Design principles
Less is more: Remove unnecessary gridlines and borders
Hierarchy matters: Use visual weight to guide the reader
Consistency: Maintain uniform formatting throughout
Tables deserve as much attention as plots in your data communication toolkit. With gt, you have the power to create tables that are not just functional, but beautiful.
Tip
Remember: A well-designed table can often communicate complex relationships more effectively than a plot. Choose your medium wisely.
Happy table-making!
Let's Transform Your Data into Impact
Evidence-based insights that drive healthcare decisions.