Use chunk labels from Rmarkdown with knitr::read_chunk()

I have two rmarkdown documents, I want to be able to pull specific code chunks from document 1 into document 2.

I know you can do this from .r scripts and .rmd if you insert a chunk label in the form of ## ---- label I am wondering if it is possible to do this using the named chunks in document 1.

Currently I am doing this in document one:

``` {r data_read}
##  ---- data_read_chunk
penguins <- read.csv("penguins.csv")
```

and this in document 2:

```{r, include=FALSE, cache=FALSE}
knitr::read_chunk(path = "document_1.Rmd")
```

```{r data_read_chunk, eval=FALSE}

```

What I would like to be able to do is be able to use the 'data_read' chunk label like this:

``` {r data_read}
penguins <- read.csv("penguins.csv")
```

and have this in document 2:

```{r, include=FALSE, cache=FALSE}
knitr::read_chunk(path = "document_1.Rmd")
```

```{r data_read, eval=FALSE}

```
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.