Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a code block of pseudocode in R Markdown/knitr?

I've tried using backticks and tildes, but they require languages. I just want a formatted code block of plain text with highlighted background.

Using eval=FALSE and tidy=FALSE works until reaching a keyword such as if, the subsequent code will be color highlighted in both the .Rmd file and the output PDF. Not specifying a language through the options removes the capability of background highlighting the code in the output.

```{r, eval=FALSE,tidy=FALSE}
loop through each species
    loop through each measurement of the current species
         if measurement ...
etc.
```
like image 327
TomNash Avatar asked Jan 20 '16 14:01

TomNash


People also ask

How do I insert a code block in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS). There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options.

What is knitr R Markdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

How do you name code chunks in R Markdown?

Code chunks in an R Markdown document contain your R code. All code chunks start and end with ``` – three backticks or graves.


1 Answers

Here's some sample code that renders to HTML just fine. It also renders correctly to Word and 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.
```
like image 189
Benjamin Avatar answered Oct 04 '22 19:10

Benjamin