在RStudio中存储Quarto报告YAML配置的可行方案咨询
可行,两种方案实现规范YAML生成
方案一:RStudio本地自定义函数(无需打包)
直接写个R函数生成YAML,把它加到R的启动配置里,每次打开RStudio都能调用:
- 先安装依赖的
yaml包:
install.packages("yaml")
- 定义生成YAML的函数:
generate_quarto_yaml <- function(output_file = "_quarto_config.yaml") { quarto_config <- list( format = list( html = list(theme = "cosmo"), pdf = list( `number-sections` = TRUE, `number-depth` = 3, documentclass = "scrbook", classoption = c("paper =a4", "twoside", "openright", "headsepline", "footsepline", "listof = totocnumbered", "chapterprefix = true", "firstiscover"), `include-in-header` = list(list(file = "preamble.tex")), geometry = c("top=1in", "bottom=1in", "left=1in", "right=1in", "heightrounded"), colorlinks = TRUE, toc = TRUE, `toc-title` = "Contents", lot = TRUE, lof = TRUE, `fig-cap-location` = "bottom", `fig-align` = "center", `fig-width` = 6.5, `fig-pos` = "h", linestretch = 2, `tbl-cap-location` = "top", `code-line-numbers` = TRUE, `code-block-bg` = "EFEFEF", `keep-tex` = TRUE, `link-citations` = TRUE, nocite = "@*", `author-meta` = "Eva", `title-meta` = "Title of the Report", hyperrefoptions = c("linktoc=all", "pdfwindowui", "pdfpagemode=FullScreen") ) ), execute = list( echo = TRUE, warning = FALSE, message = FALSE, error = FALSE, cache = TRUE ) ) yaml::write_yaml(quarto_config, file = output_file) cat(paste("规范YAML已生成到:", output_file, "\n")) }
- 把函数加到
.Rprofile里:- 运行
usethis::edit_r_profile()打开配置文件 - 把上面的函数代码粘贴进去,保存后重启RStudio
- 之后直接在控制台敲
generate_quarto_yaml()就能生成配置文件,还能指定输出路径,比如generate_quarto_yaml("my_report_config.yaml")
- 运行
方案二:封装到自定义R包(适合团队共享)
如果需要和团队统一规范,把函数做成自己的R包更方便:
- 用
usethis创建包框架:
install.packages("usethis") usethis::create_package("quartoTemplates")
在包的
R/目录下新建generate_yaml.R文件,把上面的generate_quarto_yaml函数放进去添加函数文档(可选,方便团队使用),用
roxygen2:
install.packages("roxygen2")
在函数上方添加文档注释:
#' 生成Quarto报告规范YAML配置 #' #' 一键生成预定义格式的Quarto YAML配置文件 #' @param output_file 输出YAML文件路径,默认是"_quarto_config.yaml" #' @export generate_quarto_yaml <- function(output_file = "_quarto_config.yaml") { # 函数内容同上 }
- 生成文档并安装包:
roxygen2::roxygenize() devtools::install()
之后团队成员安装你的包后,就能直接调用quartoTemplates::generate_quarto_yaml()生成规范配置。
内容的提问来源于stack exchange,提问作者Eva
相关产品推荐
相关产品推荐

