RMarkdown 中带有双引号的逐字代码块
Verbatim code chunks with double quotation marks in RMarkdown
如 4.7 Verbatim code chunks in @Yihui 's R Markdown Cookbook 所示,我们可以显示逐字内联表达式,
`r knitr::inline_expr("coef(summary(model))")`
,例如,在我们的 RMarkdown 输出中。
但是这个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
```
-->
双引号可以转义:
---
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"]')
如 4.7 Verbatim code chunks in @Yihui 's R Markdown Cookbook 所示,我们可以显示逐字内联表达式,
`r knitr::inline_expr("coef(summary(model))")`
,例如,在我们的 RMarkdown 输出中。
但是这个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
```
-->
双引号可以转义:
---
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"]')