gpt4 book ai didi

r - 在knitr中使用YAML header 参数

转载 作者:行者123 更新时间:2023-12-02 02:52:59 25 4
gpt4 key购买 nike

我正在使用 rmarkdown 和将其输出到 beamer 的 LaTeX 选项制作一组幻灯片。

我在幻灯片中使用了两个模板 - 一个特定于 LaTeX 选项,另一个 pandoc 模板经过我修改以适应幻灯片的某些附加功能。

我在 YAML header 中定义了一个名为 to_print 的选项,它是一个 bool 值 TRUE/FALSE,我将其传递给 pandoc 模板,告诉它添加一个包并清理用于打印的幻灯片。

我还想使用这个变量来定义文件的名称。基本想法是,我希望为我的幻灯片创建一个 .rmd 文件,然后只需更改此选项以表明它供学生打印或供我演示。

我发现我可以使用 YAML header 中的 knit 参数传递 render 函数,但我必须指定 to_print = TRUE 并相应地设置output_file 中的ifelse() 语句中的条件。

目前我有:

---
title: "Introduction to R"
subtitle: "Reading and saving data in R"
date: '`r format(Sys.Date())`'
output:
beamer_presentation:
fonttheme: professionalfonts
highlight: tango
includes:
in_header: "../../templatefiles/beamer_header.tex"
template: "../../templatefiles/beamer_template.tex"
incremental: no
keep_tex: yes
slide_level: 3
theme: Warsaw
toc: yes
fontsize: 10pt
fontenc: T1
to_print: TRUE
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(TRUE,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })
---

我只想指定一个参数,然后用它来表示这是否是要打印的讲义:

伪代码:

to_print: TRUE
knit: (function(inputFile, encoding) {rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(YAML_PARAM$to_print,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })

这可能吗,或者类似的东西可以让我拥有一个带有 bool 值的 .rmd 文件来切换演示与打印?

最佳答案

您可以使用rmarkdown::yaml_front_matter()函数:

---
title: "Introduction to R"
subtitle: "Reading and saving data in R"
date: '`r format(Sys.Date())`'
output:
beamer_presentation:
fonttheme: professionalfonts
highlight: tango
includes:
in_header: "../../templatefiles/beamer_header.tex"
template: "../../templatefiles/beamer_template.tex"
incremental: no
keep_tex: yes
slide_level: 3
theme: Warsaw
toc: yes
fontsize: 10pt
fontenc: T1
to_print: TRUE
knit: (function(inputFile, encoding) { rmarkdown::render(inputFile, encoding = encoding,
output_file = file.path(ifelse(rmarkdown::yaml_front_matter(inputFile)$to_print,
gsub("\\..*","_handout.pdf", inputFile),
gsub("\\..*", ".pdf", inputFile)))) })
---

关于r - 在knitr中使用YAML header 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36642411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com