You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从bslib主题中提取字体?解决bs_get_variables无返回问题

解决bs_get_variables无法提取bslib主题字体变量的问题

问题原因

你使用的font-family、base_font等变量名不符合bslib主题的实际命名规则:

  • CSS层面的字体变量以font-family-为前缀,比如font-family-base(正文字体)、font-family-heading(标题字体)、font-family-monospace(代码字体)
  • base_font、heading_font、code_font是bslib主题在R层面的配置参数,不属于CSS变量范畴,无法通过bs_get_variables()提取

解决方案

1. 提取CSS层面的字体变量

先确认主题包含的所有字体变量,再精准提取:

# 获取sketchy主题的全部变量
all_theme_vars <- bs_get_variables(bs_theme(bootswatch = "sketchy"))
# 筛选出所有字体相关的CSS变量
font_css_vars <- all_theme_vars[grepl("font-family", names(all_theme_vars))]
print(font_css_vars)

也可以直接指定正确的变量名提取:

target_vars <- c("font-family-base", "font-family-heading", "font-family-monospace")
font_result <- bs_get_variables(bs_theme(bootswatch = "sketchy"), varnames = target_vars)
print(font_result)

2. 获取R层面的字体配置参数

如果要获取base_font这类R层面的设置,直接访问主题对象的属性即可:

my_theme <- bs_theme(bootswatch = "sketchy")
# 提取R层面的字体配置
base_font <- my_theme$base_font
heading_font <- my_theme$heading_font
code_font <- my_theme$code_font

内容的提问来源于stack exchange,提问作者MLEN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:57:38