Embed one chunk in another chunk not work

Hi,

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:

imputation <- function(data) {
    <<fitting>>
    <<imputing>>
    <<score>>
    <<score_der>>
    <<s_mis>>
    <<d>>
    <<analysis>>
    <<u>>
    <<tau>>
}

It reported error with
Error: unexpected input in: "imputation <- function(data) { <<"

Is there anything wrong with my code?

Thanks!

You can't run the code. It should, however, knit without trouble.

Thanks! It works with knit.

Hello @Chenyan ,

in addition to the remark of Jake (@elmstedt ):

you can show a chunk in your RStudio Community post by enclosing them in a section with four backticks.
I think your chunk would then show like

```{r, fitting, eval = FALSE}
imputation <- function(data) {
    <<fitting>>
    <<imputing>>
    <<score>>
    <<score_der>>
    <<s_mis>>
    <<d>>
    <<analysis>>
    <<u>>
    <<tau>>
}
```

I assume that the <<name>> constructs mean that you want to include a chunk name.
You can not do that in an R function and that is what you specify by
imputation <- function(data) { ... }
I suggest you take a look at https://rmarkdown.rstudio.com/docs/articles/rmarkdown.html and the beautiful resource https://bookdown.org/yihui/rmarkdown/ that is mentioned there.

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.

Hi @HanOostdijk,

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.

Hello @Chenyan,

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:

This topic was automatically closed 7 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.