如何使用R的gt包在表格标题区域添加本地PNG图片
gt表格标题区域插入本地PNG图片的实现方法
你可以使用gt内置的local_image()函数加载本地PNG资源,再配合简单的HTML布局标签控制图片的位置,即可实现在标题左上角/右上角插入图片的需求,具体实现代码如下:
1. 图片插入标题左上角
library(gt) gt(mtcars) %>% tab_header( title = md( paste0( "<div style='display:flex;align-items:center;gap:12px;'>", # 替换为你的本地PNG路径,height参数控制图片高度 gt::local_image(filename = "本地图片路径.png", height = 30), "<span>**2014 - 2019 Salary and Playoff Appearances**</span>", "</div>" ) ) )
2. 图片插入标题右上角
调整布局样式,给图片容器加左外边距自动撑开即可实现右对齐:
library(gt) gt(mtcars) %>% tab_header( title = md( paste0( "<div style='display:flex;align-items:center;gap:12px;'>", "<span>**2014 - 2019 Salary and Playoff Appearances**</span>", # margin-left:auto实现图片右对齐 "<div style='margin-left:auto;'>", gt::local_image(filename = "本地图片路径.png", height = 30), "</div>", "</div>" ) ) )
补充说明:
- 可以调整
height参数修改图片大小,调整gap参数修改图片和标题文字的间距- 如果要使用在线图片,将
gt::local_image替换为gt::web_image("图片链接", height = 30)即可
内容的提问来源于stack exchange,提问作者Laura
相关产品推荐
相关产品推荐

