Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I modify yaml instructions outside of the document I am rendering

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
like image 987
statquant Avatar asked Oct 15 '25 15:10

statquant


2 Answers

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"))
like image 102
eipi10 Avatar answered Oct 18 '25 04:10

eipi10


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="/"))
like image 36
jay.sf Avatar answered Oct 18 '25 04:10

jay.sf