4

I'd like to rmarkdown::render a R document without indicating the yaml options within the document itself.

Ideally that could be an argument on rmarkdown::render or knitr::spin like what you can do to pass params (see the Rmarkdown reference book). Typically I'd like author, date and the output options too.

I think this is possible because spining the following document without specifying anything I get the following output (so there must be a template of default args that I can hopefully change)


enter image description here


As an example, how could I do to render a document that would give me the same output as say the below (but of course without specifying the yaml in the document ie no yaml whatsoever in the document)

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
  pdf_document:
    toc: true
    highlight: zenburn
---

#' # Title
Hello world

#+ one_plus_one
1 + 1
statquant
  • 13,672
  • 21
  • 91
  • 162

2 Answers2

6

You can pass yaml options as parameters too. For example:

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

This is my document text.

Then, in a separate R script:

rmarkdown::render("my_doc.rmd", 
                  params=list(title="My title", 
                              author="eipi10"))
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Yes +1 , I knew that, but what I want is to have **NO** yaml block in the document, sorry if that was not clear enough – statquant Jan 11 '20 at 22:56
  • How could this work for params$output? It is not a string and I think it is causing me troubles at getting it from `params$` – Cris May 26 '22 at 20:10
2

You could cat a sink into a 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="/"))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • hum yes I could actually build the entire yaml like this, it's good idea – statquant Jan 11 '20 at 23:11
  • Or check option keep .tex file in RStudio, exploit the code of a sample and use [in_header](https://bookdown.org/yihui/bookdown/yaml-options.html) option somehow, perhaps combined with my answer. – jay.sf Jan 11 '20 at 23:12