I meet a problem when I try to embed one chunk in another chunk in rmarkdown. However, it did not work. I have included the knitr, bookdown and rmarkdown packages.
I have set up my r chunk with the format below
{r, fitting, eval = FALSE}
(for showing the format here I did not add ```)
Below is my code:
And of course you can use different functions (but not chunks) in your imputation function:
```{r, fitting, eval = FALSE}
imputation <- function(data) {
myfitresult <- fit_the_data(data) # your function fit_the_data should be defined
...
}
```
Let us know if this make sense to you. If you can be more specific about what you want to achieve, this would be helpful.
Actually, below is just to show how I write the R chunk before the function chunk, which is the first R chunk I included in the function.
```{r, fitting, eval = FALSE}
Also, I have already read through Learn R Markdown • rmarkdown. In chapter 14.1 Reuse code chunks | R Markdown Cookbook, it suggests we could include other code chunks in function by enclosing its label in <<>>. It also give an example. So I did it in the same way as the example. However, it does not work. But it could work when knit.
I have not seen this construction before and when I use it RStudio flags it as incorrect.
But in fact, it does work for me. See the two screenshots (of Rmd input and HTML output) for the example in the cookbook. Thanks for pointing me to this feature.
If it does not work for you, it could be because the name of your main chunk is fitting and you try to include in it a chunk that is named identical (fitting).
This syntax is indeed not recognized in RStudio IDE yet. It will work when knitting because knitr knows how to handle it but the RStudio IDE don't when using a Rmd interactively.
There is currently a feature request for better support of this I believe: