3

My Rmarkdown document looks something like this.

---
yaml metadata
---

```{r}
x <- 10
```

Some code explanation

```{r}
y <- 10
```

Some more code explanation

```{r}
z <- x + y
```

The final output

```
# 10
```

Since I am following the concepts of literate programming, how to print these multiple code chunks stitched together, so I can get the whole working code printed out as follows without the code explanation. Also, can I select specific code chunks and not all and print them out?

x <- 10
y <- 10
z <- x + y
Shantanu
  • 839
  • 13
  • 27

1 Answers1

5

A trick is to use knitr's ref.label="" chunk option (which takes one or more block labels). It requires that you label your chunks (at least the ones you want to repeat). For demonstration, I've "hidden" (echo=FALSE) one of the blocks to show that the output can be offset (as in https://stackoverflow.com/a/30243074/3358272) though it is still executed in-place.

---
output: md_document
---

```{r block1}
x <- 10
```

Some code explanation, next code block hidden here but still evaluated

```{r block2, echo = FALSE}
y <- 10
```

Some more code explanation

```{r block3}
z <- x + y
```

The final output

```
# 10
```

# Annex 1

Each block individually:

```{r showblock1, ref.label='block1', eval=FALSE}
```
```{r showblock2, ref.label='block2', eval=FALSE}
```
```{r showblock3, ref.label='block3', eval=FALSE}
```


# Annex 2

If you want them more compactly concatenated:

```{r showblocks, ref.label=c('block1','block2','block3'), eval=FALSE}

Produces this markdown file when rendered:

    x <- 10

Some code explanation, next code block hidden here but still evaluated

Some more code explanation

    z <- x + y

The final output

    # 10

Annex 1
=======

Each block individually:

    x <- 10

    y <- 10

    z <- x + y

Annex 2
=======

If you want them more compactly concatenated:

    x <- 10
    y <- 10
    z <- x + y

You can render into whatever format you want, the results should be similar.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Perfect! This and the other answer helps a lot. – Shantanu Oct 23 '18 at 16:04
  • 1
    Your question prompted me to learn that `ref.label=` can take more than one argument. Learn something new every day. – r2evans Oct 23 '18 at 16:08
  • Hey r2evans, How can I create a SQL UNION query out of something like this? ```{sql block1, ref.label = 'block1', connection = con, eval=FALSE} SELECT TOP 5 * FROM EMPLOYEES_AMERICAS ``` ```{sql block2, ref.label = 'block2', connection = con, eval=FALSE} SELECT TOP 5 * FROM EMPLOYEES_EUROPE ``` ```{sql, ref.label=c('block1','block2'), eval=FALSE} ``` – Shantanu Nov 05 '18 at 15:54
  • Sorry for the formatting, I am unable to get it to edit it properly here. I am trying to figure out how to add the word "UNION" in between the two queries. – Shantanu Nov 05 '18 at 15:59
  • 1
    Those are two queries in two chunks. Each query is sent individually to the sql server, I believe, so if you want them combined you must do it in one query. Regardless, this has little to do with with question, I suggest you ask a new question. – r2evans Nov 05 '18 at 16:08