YAML parser error when rendering Quarto document - r

I was trying to re-render a Quarto document after adding some more code. The code previously rendered fine.
However, this time the render failed, with this error:
Error in yaml::yaml.load(meta, handlers = list(expr = parse_only)) :
Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 2, column 27
Calls: .main ... FUN -> parse_block -> partition_chunk -> <Anonymous>
Execution halted
I thought this was referring to the YAML bit at the top, but I hadn't made any changes to that, and the document previously rendered fine. I simplified the YAML to the simplest case, but the error persisted.
---
title: "Test"
format: html
---
```{r}
#| label: tbl-exibble
#| tbl-cap: "Exibble Bla Bla")
gt::gt(exibble)
```
This was caused by a typo - I'm posting this and answering it here because I think the error message is not particularly helpful and it might mislead other people

The bug lies in the table caption chunk option, not in the YAML block at the start.
I had an extra ) at the end of the line; this fixed it:
---
title: "Test"
format: html
---
```{r}
#| label: tbl-exibble
#| tbl-cap: "Exibble Bla Bla"
gt::gt(exibble)
So if you get YAML parser errors similar to mine, please check your YAML; not only in the top part of the Quarto document, but also in your chunk options.

You do not even need ", e.g.
---
title: "Test"
format: html
---
```{r}
#| label: tbl-exibble
#| tbl-cap: Exibble Bla Bla)))))
gt::gt(mtcars)
```
inline code
---
title: "Test"
format: html
---
```{r}
#| label: tbl-exibble
#| tbl-cap: !expr paste0("mtcars")
gt::gt(mtcars)
```

Related

Quarto PDF output: code block line spacing

This Github repo, hosts a .qmd file of my dissertation template. In config/preamble.tex I've set \onehalfspacing and \linespread{1.5} which I thought would affect only plain text and not also code blocks.
Is it possible to change monofont spacing individually (or inversely, set space for mainfont only)?
More quarto way to do this actually modifying the knitr chunk hook.
---
title: ""
format:
pdf:
include-in-header:
text: |
\usepackage{lipsum}
\usepackage{setspace}
\onehalfspacing
\linespread{2}
df-print: kable
highlight-style: zenburn
fontsize: 12pt
geometry: margin=1in
---
```{r}
#| label: setup
#| include: false
chunk_hook <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
x <- chunk_hook(x, options)
paste0("\\linespread{0.5}\n", x, "\n\n\\linespread{2}")
})
```
## Different linespacing for text and code
When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
```{r}
library(dplyr, quietly = TRUE)
mtcars %>%
group_by(am) %>%
summarise(
disp = mean(disp),
mpg = mean(mpg)
)
```
\lipsum[1]
Here I have used linespace 0.5 for code and linespace 2 for text. Change these as you need.
Code blocks are defined in Shaded environment. So, the fix was simply redefining it in preamble.tex using singlespace environment and \linespread{1}:
\renewenvironment{Shaded}
{\begin{snugshade}
\begin{singlespace}
\linespread{1}
}
{\end{singlespace}
\end{snugshade}
}

using params from YAML header with a shiny runtime Rmd

I would like to use params to pass settings for a shiny app. Consider the following document, which just prints a message when the checkbox is changed. I would like the message to be configuable using params in the yaml header.
However, it seems this fails as params seems to be only available when the document is rendered (notice that it correctly prints the message in the document itself). Is this correct? Is there any way of reading params at runtime? Is this documented anywhere?
Document is below:
---
title: "Test"
output: html_document
runtime: shiny
params:
message: "hi"
---
Can see this: `r params$message`
```{r eruptions, echo=FALSE}
shiny::checkboxInput("cb","Checkbox")
```
```{r, echo=FALSE}
observeEvent(input$cb, {
# This works
message("clicked")
# This fails:
# Error in message(params$message) : object 'params' not found
try(message(params$message))
})
```
This works by setting params to a non-local variable:
---
title: "Test"
output: html_document
runtime: shiny
params:
message: "hi"
---
```{r}
params <<- params
```
......

R Markdown code chunk does not show math equations

Basically i want to build a random multiple choice question generator with R Markdown. For this task there need to be equations in the code chunks of the markdown.
The following works like a charm and gives the equation "greekbeta = 1"
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\beta = 1$"
```
In contrast, this will not work when some other math symbol is used, for example:
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
"$\sum_{n=1}^{\infty}$"
```
After pressing knit, an error occurs (unfortunately the error message is in german, basically this: "'\s' is an unknown escape-sequence within the string starting with "$/s"").
I am very puzzled by this, especially because for example \frac{1}{2} works, but \hat{x} does not. Equations in the "normal" markdown text are no problem at all. But for my task, the equations have to be in the code chunk sections.
Does someone has a workaround for this problem? I tried using "$\hat{x}$" or even "$$\hat{x}$", but the error message is still the same.
I am using pandoc 2.11.4, R 4.1.0 and knitr 1.33
Use cat() and escape the escapes.
---
title: "test"
author: "me"
output:
word_document: default
---
```{r eval=TRUE, echo=FALSE,results = "asis"}
cat("$\\beta = 1$", '\n\n')
cat("$a^2+b^2 = c^2$", '\n\n')
cat("$\\sum_{n=1}^{\\infty}x_i$", '\n\n')
```

Text (not syntax) highlighting in Rmarkdown/Papaja

I'm compiling a report using Papaja and Rmarkdown, and I want to highlight various text throughout. Doing so with latex or pure Rmarkdown is easy, but I'm receiving an "undefined control sequence" error when I compile the report to PDF using Papaja's application of Knitr.
A similar question about text highlighting within a single Rmarkdown file was answered here: RMarkdown / pandoc fails to knit Pdf with latex color commands. I'd like to know if an answer exists for multiple Rmarkdown files using Papaja.
I'll include a minimal example below.
1) File called papaja_file.Rmd
---
# https://crsh.github.io/papaja_man/index.html
title : "Some Title"
shorttitle : "HEADER"
author:
- name : "Name R Here"
affiliation : "1"
corresponding : yes # Define only one corresponding author
address : "Include addresss"
email : "randomemail#usa.edu"
affiliation:
- id : "1"
institution : "Any University"
author_note: |
....
abstract: |
Text here for abstract.
keywords : "Keyword1, keyword2, keyword3"
bibliography : ["references.bib"]
figsintext : no
figurelist : no
tablelist : no
footnotelist : no
lineno : yes
lang : "english"
class : "man"
output : papaja::apa6_pdf
---
```{r load_packages, include = FALSE}
library("papaja")
```
```{r analysis_preferences}
# Seed for random number generation
set.seed(42)
```
```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', echo = TRUE, warning = FALSE, message = FALSE)
```
```{r child = 'child_document.Rmd'}
```
\newpage
# References
```{r create_references, echo = F}
r_refs(file = "references.bib")
```
\setlength{\parindent}{-0.5in}
\setlength{\leftskip}{0.5in}
Notice that it references a single child Rmarkdown document.
2) The child document with text, called child_document.Rmd
---
output:
pdf_document:
includes:
in_header: preamble.tex
---
One sentence without highlighting. \hl{Another with highlighting.}
3) The preamble, called preamble.tex
\usepackage{color}
\usepackage{soul}
\definecolor{lightblue}{rgb}{0.90, 0.95, 1}
\sethlcolor{lightblue}
Knitting the main papaja file produces the error. Removing the \hl command within the child document allows the pdf to compile without issue.
Result after putting YAML header within the main papaja document rather than the child:
The YAML header in your child document is not evaluated, c.f.
The master document, for example, consists of the YAML front matter and includes the children, which are themselves R Markdown documents without a YAML front matter.
https://crsh.github.io/papaja_man/tips-and-tricks.html#splitting-an-r-markdown-document
But you can include your preamble.tex in you master file using:
output:
papaja::apa6_pdf:
includes:
in_header: preamble.tex
c.f. https://github.com/rstub/stackoverflow/commit/a92a67d4721ee9c06e995b08adbf8cb89daebcb4
Result:

R Markdown error

I am trying to create an R markdown file but when I do I get the following error:
Line 2:
Error in .Call(C_str Sub_replacement, str, from, to, NULL, value) :
Incorrect number of arguments (5), expecting 6 for 'C_str Sub Replacement'
Calls: <Anonymous> ... indir -> inline exec -> <Anonymous> -> str_sub<- -> .Call
I have tried searching the internet and can not find an answer to this specific error. I am running R-3.3.0 and using R Studio.
I have updated the packages - knitr, markdown and yaml.
The following is my set up:
---
title: "Setting up a DQASS PC"
author: "DKMillington"
date: "`r format(Sys.Date(), '%d/%m/%Y')`"
header-includes:
- \usepackage{amsmath}
output:
rmarkdown::html_vignette:
css: mystyle.css
toc: true
number_sections: true
---
```{r, echo = FALSE, message = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(dqass)
```
Thank you for your time in advance.
Dale

Resources