如何转义 RMarkdown 块? [英] How do I escape an RMarkdown chunk?

查看:42
本文介绍了如何转义 RMarkdown 块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RMarkdown 中代码块的语法呈现为 pdf.最终输出应该看起来像

```{r}#一些代码``

而不仅仅是

#一些代码

解决方案

可以在 RStudio 网站上找到回答此问题的最佳选项.请参阅有关将逐字 R 块转换为 rmarkdown 的文章 http://rmarkdown.rstudio.com/articles_verbatim.html

我的首选解决方案是在第一行的末尾添加一个内联 R 代码,以使块在输出中正确显示,如下所示:

```{r eval=TRUE}` r ''`序列(1, 10)``

产生什么

```{r eval=TRUE}序列(1:10)``

而不是

## [1] 1 2 3 4 5 6 7 8 9 10

做同样事情的另一种方法是将代码块视为字符串(我通常不使用 -> 运算符,但在这种情况下,我认为这可以提高可读性):

```{r comment = ""}"{r 评估=真}序列(1, 10)" -> my_code_stringcat("```", my_code_string, "```", sep = "")``

输出:

```{r eval=TRUE}序列(1, 10)``

这个问题也可能是如何在 R Markdown 文档中嵌入和转义 R Markdown 代码

I am trying to render the syntax for chunks of code in RMarkdown to a pdf. The final output should look like

```{r}
#some code
```

Rather than just

#some code

解决方案

The best options to answer this can be found on the RStudio website. See the article on getting verbatim R chunks into rmarkdown http://rmarkdown.rstudio.com/articles_verbatim.html

My preferred solution is to add an inline R code at the end of the first line to make the chunk appear correctly in the output like so:

```{r eval=TRUE}` r ''`
seq(1, 10)
```

Which produces

```{r eval=TRUE}
seq(1:10)
```

Rather than

##  [1]  1  2  3  4  5  6  7  8  9 10

Another way to do the same thing is treat the code chunk as a string (I don't usually use the -> operator but in this case I think this improves readability):

```{r comment = ""}
"{r eval=TRUE}
seq(1, 10)
" -> my_code_string

cat("```", my_code_string, "```", sep = "")
```

Which outputs:

```{r eval=TRUE}
seq(1, 10)
``` 

This question is also a possible duplicate of How to embed and escape R Markdown code in a R Markdown document

这篇关于如何转义 RMarkdown 块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆