简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM