1

正如@Yihui 的R Markdown Cookbook中的4.7 Verbatim 代码块所示,我们可以在 RMarkdown 的输出中显示逐字内联表达式, 例如。但是,这将无法解析带有双引号的代码,例如 . 那么,当我想逐字演示包含这种特殊字符的内容时,我应该怎么做呢?`r knitr::inline_expr("coef(summary(model))")`knitr::inline_expr()`r knitr::inline_expr("coef(summary(model))["(Intercept)", "Estimate"]")`

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

1 回答 1

4

您可以转义双引号:

---
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\"]")`

在此处输入图像描述

在评论中,@monte 提供了另一种解决方案,即交替单引号和双引号:knitr::inline_expr('coef(summary(model))["(Intercept)", "Estimate"]')

于 2020-06-20T13:33:56.430 回答