R 如何在正在呈现的文档之外修改yaml说明

R 如何在正在呈现的文档之外修改yaml说明,r,r-markdown,knitr,R,R Markdown,Knitr,我想rmarkdown::呈现R文档,而不指示文档本身中的yaml选项 理想情况下,这可以是rmarkdown::render或knitr::spin上的一个参数,就像传递参数一样(请参阅)。通常我想要作者,日期和输出选项 我认为这是可能的,因为在不指定任何内容的情况下旋转参数会得到以下输出(因此必须有一个默认参数模板,我希望可以更改该模板) 例如,我如何呈现一个文档,该文档将为我提供与下面所述相同的输出(但当然不指定文档中的yaml,即文档中没有任何yaml) 您也可以将yaml选项作为

我想
rmarkdown::呈现
R
文档,而不指示文档本身中的yaml选项

理想情况下,这可以是
rmarkdown::render
knitr::spin
上的一个参数,就像传递
参数一样(请参阅)。通常我想要
作者
日期
输出
选项

我认为这是可能的,因为在不指定任何内容的情况下旋转参数会得到以下输出(因此必须有一个默认参数模板,我希望可以更改该模板)



例如,我如何
呈现一个文档,该文档将为我提供与下面所述相同的输出(但当然不指定文档中的yaml,即文档中没有任何yaml


您也可以将yaml选项作为参数传递。例如:

---
params: 
  title: "add title"
  author: "add author"
output: pdf_document
title: "`r params$title`"
author: "`r params$author`"
---

This is my document text.
然后,在单独的R脚本中:

rmarkdown::render("my_doc.rmd", 
                  params=list(title="My title", 
                              author="eipi10"))

您可以将
cat
a
sink
放入
tempfile

xxx <- "
#' # Title
Hello world

#+ one_plus_one
1 + 1
"

tmp <- tempfile()
sink(tmp)
cat("
---
title: 'Sample Document'
output:
  html_document:
    toc: true
    theme: united
  pdf_document:
    toc: true
    highlight: zenburn
---", xxx)
sink()
w.d <- getwd()
rmarkdown::render(tmp, output_file=paste(w.d, "myfile", sep="/"))

xxx是+1,我知道,但我希望文档中没有yaml块,抱歉,如果不清楚的话。嗯,是的,我可以像这样构建整个yaml,这是一个好主意或检查选项,在RStudio中保留.tex文件,以某种方式利用示例代码并使用选项,也许结合我的答案。
xxx <- "
#' # Title
Hello world

#+ one_plus_one
1 + 1
"

tmp <- tempfile()
sink(tmp)
cat("
---
title: 'Sample Document'
output:
  html_document:
    toc: true
    theme: united
  pdf_document:
    toc: true
    highlight: zenburn
---", xxx)
sink()
w.d <- getwd()
rmarkdown::render(tmp, output_file=paste(w.d, "myfile", sep="/"))