Problem
When trying knit
.Rmd, containing the lines read_chunk
from the purl
scripts, to the parent .Rmd, the pieces are not complete and form only blocks of code. I want to be able knit
to output the file normally.
The code
main.Rmd
---
output: html_document
---
```{r, include=FALSE}
knitr::read_chunk("script_chunk.R")
```
```{r, ref.label='script_chunk', eval=FALSE}
```
script_chunk.R
sessionInfo()
Knitting
When I process this with knit("main.Rmd", "output.Rmd")
, the following file is created:
---
output: html_document
---
### Print sessionInfo()
```r
sessionInfo()
```
However, the desired output for the block:
```{r script_chunk}
sessionInfo()
```
When I knit
output.Rmd at the moment, I get only an unappraised code block because the piece does not have curly braces (and preferably the name of the piece).

Bypass
I can use readLines
to achieve what I need, for example:
```{r, results='asis', collapse=TRUE, echo=FALSE}
cat("```{r script_chunk}\n")
cat(paste(readLines("script_chunk.R"), "\n", collapse = ""))
cat("```\n")
```
Is there a more elegant way to do this?