我有一个参数化的Quarto文档,并希望自动渲染,并使用YAML中的一些参数以及Sys.Date()来命名生成的html文件,以便每次渲染Quarto文档时,输出的html文件不会覆盖前一个.

我在Quarto中try 了适用于RMarkdown显示here的解决方案,但没有成功,因为在Quarto中的YAML中使用键Knit时似乎不存在,所以html使用与.qmd文件相同的名称.以下是我根据上面提到的链接try 的最小示例

---
title: "Example"
author: "Anthony"
date: "`r Sys.Date()`"
output: html_document
params:
   variable: "var1"
knit: >
  (function(input_file, encoding) {
    metadata <- rmarkdown::yaml_front_matter(input_file)
    output_file <- with(metadata, paste0(title,"_",params$variable, "_", author, "_",date))
    rmarkdown::render(input = input_file, output_file = output_file)
---

我还try 了用于Quarto文档的output-fileoutput-ext键(如图here所示),但它只能用于没有类似代码的字符串,因此下面的YAML也不起作用.

---
params:
   variable = "variable1"
format:
   html: 
    output-file: "`r paste0("file_name","_",params$variable)`"
    output-ext:  "html"
title: "Testing Output filename"
--- 

在上一个YAML中try output-file: "File {{< meta params.variable>}}"也不起作用(尽管这种方法在title:键中使用时确实有效).

因此,如果可能的话,我想通过在YAML中添加相关代码来自动生成html文件名.感谢您的帮助,谢谢!

推荐答案

这可能是一种简陋的方式来完成您想要的操作,但是为什么不创建一个带有变量占位符的模板QMD文件呢?由于它只是一个纯文本文档,您可以使用R来阅读它,然后用您需要的文本(日期等)替换占位符,然后使用正确的文件名和日期生成一个新的QMD文件.该文件可以使用quarto::quarto_render("document.qmd")进行渲染.大致是这样的:

library(tidyverse)
library(quarto)

# you need to change the path to your template file
quarto_code_template <- read_file(r"{C:\Users\novot\Desktop\template.txt}")

output_file_name <- Sys.Date() %>%
  as.character() %>%
  paste0(".qmd")

# replacing the date placeholder with the actual date
quarto_code_actual <- quarto_code_template %>%
  str_replace("::date_placeholder::", as.character(Sys.Date()) )

# generating the quarto code file
write_file(quarto_code_actual, output_file_name)

# rendering an html document from the quarto file
quarto::quarto_render(output_file_name)

模板文件编码如下:

---
title: "Example"
author: "Anthony"
date: "::date_placeholder::"
output: html_document
---

Html相关问答推荐

关于角内按钮范围的混乱

R中的动态Webscraping

你能让Cypress测试为p—fileUpload做文件上传吗?

Select 包含iframe的图形<><>

如何从ThymeLeaf模板中分离CSS

只有一行上有溢出的CSS网格

CURL邮箱中的图像不能调整其大小

如何收集<;p&>元素下的<;p>;子元素

如何使用CSS在NSAttributedString中为HTML文本中的图像制作覆盖图

带下划线的文本应该有延长的下划线,以使其图像悬停

带有下拉菜单的完整css导航-链接不起作用?

如何将tmap按钮和图例更改为位于页脚后面?

多次使用组件时计数Angular 14中嵌套数组的指令

Swift WKUserContentController 和 WKScriptMessageHandler 设置不正确

如何从通过 find-each 方法(在 Rails 应用程序中)生成的列表创建下拉菜单?

Div 容器不会遵守边距、填充或间隙

CSS 中的 fingerprint scanner 动画

在随机图像下对齐文本

在 React (NEXT) 中,当我try 进行动态补充时,我无法使用实例this来引用输入

如何突出显示 .qmd html 文件中的特定代码行