Verbatim code chunks with double quotation marks in RMarkdown - r

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"]')

Related

I am having trouble while Knitting RMarkdown file

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

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')
```

R Markdown keywords don't appear in the document

I want to knit a pdf-document but the keywords don't appear in the final document. Can someone say me what I'm doing wrong?
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
keywords: "word1, word2, word3"
footerdate: yes
abstract: 'Insert abstract here'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction
When knitting to PDF, keywords are sent to the file metadata but are not actually visible in the file. Source
You can customize the template for PDF generation or render Rmd files with base_format: rticles::elsevier_article.
However, there is another way to show keywords on the PDF just by adding few codes to the Rmd file:
Add a LaTeX code to provide the \keyword command (\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}) into YAML's header-includes key;
Add keywords in the text section using the LaTeX command
---
title: "title"
subtitle: "subtitle"
author: "author"
date: "09 04 2019"
output:
pdf_document:
# keywords: "word1, word2, word3" # <- You don't have to add your keywords here since they only appear as the 'invisible' metadata of PDF
footerdate: yes
abstract: 'Insert abstract here'
header-includes:
- |
```{=latex}
\providecommand{\keywords}[1]{\textbf{\textit{Keywords---}} #1}
```
---
```{=latex}
\keywords{word1, word2, word3}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Introduction

Comment control from YAML in R Markdown

I'm trying to control a comment from my YAML header with a parameter, but I can't seem to make it work.
Here is an example of my code.
---
title: My report
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document: default
pdf_document: default
word_document: default
params:
optional_text: "TRUE"
---
`r if(optional_text){"My text"}`
I have tried a few things (with/without quotation marks, etc.), but I always end up with the same error
Error in eval(parse_only(code[i]), envir = envir) :
object 'optional_text' not found
I have figured out a walkaround in the meanwhile, but it just doesn't seem efficient.
```{r label, include=FALSE}
optional_text<-TRUE
```
You just have to change one little thing:
`r if(params$optional_text){"My text"}`
In RMD the parameters are called with params$NAME_OF_PARAMETER, see here: http://rmarkdown.rstudio.com/developer_parameterized_reports.html

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