Using a YAML header argument in knitr
Asked Answered
K

1

9

I am making a set of slides using rmarkdown and the LaTeX option of outputting it to beamer.

I have two templates I use in my slides - one specific for the LaTeX options, and one pandoc template that I have modified to account for some additional feature of my slides.

I have defined an option in the YAML header called to_print which is a boolean TRUE/FALSE that I pass to the pandoc template that tells it to add a package and clean up the slides for printing.

I also want to use this variable to define the name of the files. The basic idea being that I want to have one .rmd file for my slides, and then just changing this one option to signify that it's for printing by students, or for me presenting.

I have figured out that I can pass the render function using the knit parameter in the YAML header, but I have to specify to_print = TRUE and set the condition in the ifelse() statement in the output_file correspondingly.

Currently I have:

---
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)))) })
---

I would like to specify just one parameter that is then used to signify if this is a handout to be printed:

Pseudo-code:

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)))) })

Is this possible, or something equivalent that would let me have one .rmd file with a boolean to toggle presentation vs. printing?

Kellar answered 15/4, 2016 at 8:47 Comment(0)
R
3

You can use the rmarkdown::yaml_front_matter() function:

---
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)))) })
---
Riebling answered 14/1, 2019 at 23:20 Comment(1)
This is exactly what I need. More documentation about custom knitting is over here: bookdown.org/yihui/rmarkdown-cookbook/custom-knit.htmlJesusitajet

© 2022 - 2024 — McMap. All rights reserved.