如何在我正在呈现的文档之外修改 yaml 指令
How can I modify yaml instructions outside of the document I am rendering
我想 rmarkdown::render
一个 R
文档,而不在文档本身 中指明 yaml 选项 。
理想情况下,这可能是关于 rmarkdown::render
或 knitr::spin
的论点,就像您可以通过 params
做的那样(参见 Rmarkdown reference book)。通常我也想要 author
、date
和 output
选项。
我认为这是可能的,因为在没有指定任何内容的情况下旋转 following document 我得到以下输出(因此必须有一个默认参数模板,我希望可以更改)
举个例子,我怎样才能 render
一个文档,它会给我与下面所说的相同的输出(但是当然没有在文档中指定 yaml 即没有文件中的任何 yaml)
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---
#' # Title
Hello world
#+ one_plus_one
1 + 1
您也可以将 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
一个 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="/"))
我想 rmarkdown::render
一个 R
文档,而不在文档本身 中指明 yaml 选项 。
理想情况下,这可能是关于 rmarkdown::render
或 knitr::spin
的论点,就像您可以通过 params
做的那样(参见 Rmarkdown reference book)。通常我也想要 author
、date
和 output
选项。
我认为这是可能的,因为在没有指定任何内容的情况下旋转 following document 我得到以下输出(因此必须有一个默认参数模板,我希望可以更改)
举个例子,我怎样才能 render
一个文档,它会给我与下面所说的相同的输出(但是当然没有在文档中指定 yaml 即没有文件中的任何 yaml)
---
title: "Sample Document"
output:
html_document:
toc: true
theme: united
pdf_document:
toc: true
highlight: zenburn
---
#' # Title
Hello world
#+ one_plus_one
1 + 1
您也可以将 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
一个 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="/"))