<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>表格 on 楚新元 | All in R</title><link>https://cxy.cc/tags/table/</link><description>Recent content in 表格 on 楚新元 | All in R</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><lastBuildDate>Mon, 11 May 2026 13:15:01 +0800</lastBuildDate><atom:link href="https://cxy.cc/tags/table/index.xml" rel="self" type="application/rss+xml"/><item><title>gt 包生成表格代码示例</title><link>https://cxy.cc/post/2026/05/11/gt/</link><pubDate>Mon, 11 May 2026 13:15:01 +0800</pubDate><guid>https://cxy.cc/post/2026/05/11/gt/</guid><description>gt 包相当于表格界的 ggplot2，之前学了很多做表的包，要么语法不够优雅自然，要么功能不够强大，现在我只需要 gt 包就够了，尽管日常使用中我们的表格没有这个复杂，但是 gt 包真的把表格的各种要素全都考虑进来了，不用再学一堆包了，另外 gt 包的语法也更统一更简洁。对于国内大量使用 Word 文档的场景，再学习下 flextable 包就基本差不了。
先来重新认识下表的构成要素：
下面是一个学习 gt 包核心功能的代码示例：
# 加载必要的 R 包 library(dplyr) library(gt) # 准备数据 mtcars |&amp;gt; tibble::rownames_to_column(&amp;quot;model&amp;quot;) |&amp;gt; select( model, cyl, disp, hp, mpg, wt, qsec ) |&amp;gt; head(10) |&amp;gt; mutate( hp1 = hp cyl = paste(cyl, &amp;quot;缸&amp;quot;) ) -&amp;gt; df # 创建 gt 表格，并进行丰富的格式化和样式调整 df |&amp;gt; gt(groupname_col = &amp;quot;cyl&amp;quot;) |&amp;gt; # 1. 表格标题 &amp;amp; 副标题 tab_header( title = md(&amp;quot;**gt 包功能全面展示**&amp;quot;), subtitle = md(&amp;quot;基于 *mtcars* 数据（前 10 车型）&amp;quot;) ) |&amp;gt; # 2.</description></item></channel></rss>