是否可以在R Markdown / knitr中创建伪代码的代码块?

时间:2016-01-20 14:45:30

标签: r knitr r-markdown

我尝试过使用反引号和波浪号,但它们需要语言。我只想要一个带有突出显示背景的纯文本代码块。

使用eval=FALSEtidy=FALSE一直有效,直到达到if等关键字后,后续代码将在.Rmd文件和输出PDF中以颜色突出显示。不通过选项指定语言会删除背景突出显示输出中代码的功能。

```{r, eval=FALSE,tidy=FALSE}
loop through each species
    loop through each measurement of the current species
         if measurement ...
etc.
```

2 个答案:

答案 0 :(得分:8)

这里有一些示例代码可以很好地呈现给HTML。它还可以正确呈现为Word和PDF。

---
title: "Untitled"
output: html_document
---

Here is some plain text.

Next, let's write some pseudo code.  Note that you don't _have_ to specify a language if using plain backticks.

```
object <- [some kind of calculation]
```

If you set `eval = FALSE` you can get the highlighted background

```{r, eval = FALSE}
object <- [some kind of calculation]
Note that this; is not valid R code
```

You may find it interesting that your example works just fine for me.

```{r, eval=FALSE}
loop through each species
    loop through each measurement of the current species
    ...
etc.
```

答案 1 :(得分:1)

使用tidy = FALSE

```{r, tidy=FALSE, eval=FALSE, highlight=FALSE }

pseudocode

```

编辑:添加突出显示,以防您统一颜色