I am having trouble while Knitting RMarkdown file - r

I am getting following error while Knit my RMarkdown file.
Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 2, column 11
Calls: <Anonymous> ... parse_yaml_front_matter -> yaml_load -> <Anonymous>
Execution halted*
I understand it's my yaml header which is following -
---
title: "Data Wrangling Assessment"
author: ""ABC 11111""
subtitle:
output:
word_document: default
html_document:
df_print: paged
html_notebook: default
---
Can anyone please help me to troubleshoot?

The issue is that you wrapped the author name in doubled double quotes. If you want to quote the author name you have to
escape the inner double quotes using \", e.g. "\"ABC 11111\""
replace the outer double quotes with single quotes, e.g. '"ABC 11111"'
---
title: "Data Wrangling Assessment"
author: "\"ABC 11111\""
subtitle:
output:
word_document: default
html_document:
df_print: paged
html_notebook: default
---

Related

Verbatim code chunks with double quotation marks in RMarkdown

As demonstrated in 4.7 Verbatim code chunks in #Yihui 's R Markdown Cookbook, we can show a verbatim inline expression,
`r knitr::inline_expr("coef(summary(model))")`, for example, in our output of RMarkdown.
However, this knitr::inline_expr() would not be able to parse a code with double quotation marks, such as
`r knitr::inline_expr("coef(summary(model))["(Intercept)", "Estimate"]")`.
Then, what should I do when I want to demonstrate the verbatim which contains such a special characters?
---
title: "Untitled"
author: "CLRR"
date: "2020/6/20"
documentclass: article
output:
bookdown::pdf_document2:
latex_engine: xelatex
keep_tex: TRUE
---
This verbatim can appear in the output:
`` `r knitr::inline_expr("coef(summary(model))")` ``
<!--
But, if the code contains `"`, the evaluation fails.
`r knitr::inline_expr("coef(summary(model))["(Intercept)", "Estimate"]")`
```
Quitting from lines 2-16 (test.Rmd)
Error in parse(text = code, keep.source = FALSE) :
<text>:1:54: unexpected string constant
1: knitr::inline_expr("coef(summary(model))["(Intercept)", "
^
Calls: <Anonymous> ... hook_eval -> withVisible -> eval -> parse_only -> parse
Execution halted
```
-->
You can escape the double quotes:
---
title: "Untitled"
author: "CLRR"
date: "2020/6/20"
documentclass: article
output:
bookdown::pdf_document2:
latex_engine: xelatex
keep_tex: TRUE
---
This verbatim can appear in the output:
`r knitr::inline_expr("coef(summary(model))[\"(Intercept)\", \"Estimate\"]")`
In the comments, #monte provides the other solution, which is alternating single and double quotes: knitr::inline_expr('coef(summary(model))["(Intercept)", "Estimate"]')

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)
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
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"))
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="/"))

Can't use PLOS rticles template with bookdown

Following this post, I'm trying to put #YihuiXie 's answer in practice with the PLOS template of rticles but it doesn't work. Any help would be very appreciated!
Below in a minimal example:
---
title: Title of submission to PLOS journal
author:
- name: Me
affiliation: Here
# output: rticles::plos_article
output:
bookdown::pdf_document2:
base_format: rticles::plos_article
---
# Introduction
Some text \#ref(fig:fig1)
# References {#references .unnumbered}
The error message reads
Error in pdf_book(..., base_format = rmarkdown::pdf_document) : formal argument "base_format" matched by multiple actual arguments
Calls: <Anonymous> -> create_output_format -> do.call -> <Anonymous>
Execution halted
You cannot change the base_format for bookdown::pdf_document2. You can do so for bookdown::pdf_book, though:
---
title: Title of submission to PLOS journal
author:
- name: Me
affiliation: Here
#output: rticles::plos_article
output:
bookdown::pdf_book:
base_format: rticles::plos_article
---
# Introduction
Some text \#ref(fig:fig1)
# References {#references .unnumbered}
Note to other readers: Make sure that .../rticles/rmarkdown/templates/plos_article/skeleton/PLOS-submission.eps is present in that directory.

How can we pass pandoc_args to yaml header in rmarkdown?

I'm using rmarkdown to produce a word (.docx) report. I would like to change the header of the toc. This seems possible as pandoc_args can be passed as options in the yaml header in the case of a doc file (1). But I'm not doing it right. Could anyone provide a working example ?
(1) pandoc.args is included in the rmarkdown possible options and in the pandoc manual, there is a toc-title option
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: toc-title="Table des matières"
---
# One section
# Another
This produces :
The title of the table of contents is document metadata, so you can set it with YAML metadata block.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
toc-title: "Table des matières"
---
Or passed it in with the -M command-line flag.
---
title: "yaml header"
author: "cedric"
output:
word_document:
toc: true
pandoc_args: [
"-M", "toc-title=Table des matières"
]
---

R markdown YAML dynamic variables

In RMarkdown, I seem to be able to create 'some' dynamic variables in the YAML header, but not for others:
For instance, this works:
---
title:
"Some Title, `r format(Sys.time(), '%d %B, %Y')`"
...
---
But this does NOT.
---
...
pdf_document:
keep_tex: `r 'true'`
---
But this DOES (ie not dynamic).
---
...
pdf_document:
keep_tex: true
---
So how can I 'dynamically' assign the keep_tex to either true or false, what I want to do, is something like this:
---
...
pdf_document:
keep_tex: `r getOption('mypackage.keep_tex')`
---
I don't know if the template options can be set programmatically in the YAML header of the .Rmd file.
As an alternative, if you use rmarkdown::render to render your document, you may specify the output template (pdf_document), and then set template options (e.g. keep_tex) programmatically.
For example, if you have a .Rmd file called "test.Rmd" like this:
---
title:
"Some Title, `r format(Sys.time(), '%d %B, %Y')`"
---
...and some logical object which determines whether to keep the intermediate TeX file or not, e.g.
my_keep <- TRUE
...you may render the input file to PDF format and keep the TeX file like this:
render(input = "test.Rmd",
output_format = pdf_document(keep_tex = my_keep))

Resources