How to render a r-chunk code as verbatim in rmarkdown

Viewed 45

Consider the following rmarkdown document:

---
output: pdf_document
params:
  sec1: 1
---

```{r echo = FALSE}
sec1_is_0 <- params$sec1 == 0
sec1_is_1 <- params$sec1 == 1
```
    
## section 1

```{r a, echo = sec1_is_0, eval = FALSE}
x <- 2
y <- 3

x+y
```
    
```{r b, echo = sec1_is_1, eval = FALSE}
x <- 4
y <- 5

x+y
```
  
### A

```{r a, echo = FALSE, eval = sec1_is_0, results='hide'}
```

```{r b, echo = FALSE, eval = sec1_is_1, results='hide'}
```

`r round(x+y, 3)`

### B

```{r b, echo = FALSE, eval = sec1_is_0, results='hide'}
```

```{r a, echo = FALSE, eval = sec1_is_1, results='hide'}
```

`r round(x+y, 3)`

How can we get this document rendered as a markdown file (notice markdown and not rmarkdown) such that the r-chunks converted to verbatim:

## section 1

\begin{verbatim}
x <- 4
y <- 5

x+y
\end{verbatim}

### A

9

### B

5
1 Answers

Try using output: md_document instead of output: pdf_document in the yaml frontmatter. This gives the following output:

## section 1

    x <- 4
    y <- 5

    x+y

### A

9

### B

5

Note that the indented lines are equivalent to using \begin{verbatim} and \end{verbatim}.

Related