nanounanue
nanounanue

Reputation: 8342

knit_child in a Rmd file is printing unwanted output

I had succesfully used knit_child for generating pdf files, following the code of http://yihui.name/knitr/demo/child/, but when I try to use that example in a .Rmd file:

```{r, results='asis', echo=FALSE, message=FALSE}
out = NULL
for (p in c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10")) {
  out = c(out, knit_child('quick_variable.Rmd'))
  cat(out)
}
```

(I modify the original code, for work in Rmd).

I have two problems, the first one:

|
| | 0% |
|... | 5% ordinary text without R code

|
|....... | 11% label: unnamed-chunk-4 (with options) List of 1 $ echo: logi FALSE

|
|.......... | 16% ordinary text without R code

|
|.............. | 21% label: unnamed-chunk-5 (with options) List of 2 $ echo : logi FALSE $ results: chr "asis"
.... 
(the output follows)

Obviously all this output is unwanted. I believe that this problem is related to the use of cat in the code above, but if I remove that, no output, no plots is printed. What can I do for solving this?

Thanks in advance

Upvotes: 9

Views: 1537

Answers (2)

Nathaniel Phillips
Nathaniel Phillips

Reputation: 284

Note, I believe this should now be solved with the quiet = TRUE argument which can be called in knitr::knit_child()

See https://github.com/yihui/knitr/issues/741 for a relevant thread.

Upvotes: 1

Yihui Xie
Yihui Xie

Reputation: 30114

You can collect the results in out, and write it to the output later in an inline R expression, e.g.

```{r include=FALSE}
out = NULL
for (p in c("p1","p2","p3","p4","p5","p6","p7","p8","p9","p10")) {
  out = c(out, knit_child('quick_variable.Rmd'))
}
```
`r paste(out, collapse='\n')`

Upvotes: 9

Related Questions