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