Subviews with knitr
Contents
Let’s say you have to do the same analysis with diffent data and you want to use knitr’s RMarkdown to publish the results of each analysis in one report.
The Subview or Parameterized Report
So you start writing a RMarkdown-file. You can define several variables this RMarkdown-files depends on. It’s called Parameterized Reports. As the above article describes you define these parameters in the header of the RMarkdown-file. So let’s try it:
The name of the file subview.Rmd.
|
|
As you can see we define a paramter data in line 4. We can get the value of it with params$data (see line 8).
Calling the Subview or the Main Report
So let’s create a Main Report including the Subview two times:
|
|
The name of this file is Main Report.Rmd
The result is this:
Downsides
Unfortunately there are some downsides.
-
First it looks very ugly how we set the paramters and do the calling of the subview:
1 2 3 4 5
```{r, echo=FALSE} params=list(data=cars) ``` ```{r child='subview.Rmd'} ```
I would prefer something like
1 2
```{r child='subview.Rmd', params=list(data=cars)} ```
But this is currently not supported. Maybe I will suggest it to [https://github.com/yihui/knitr/issues].
-
The subview can have side-effects: If you change the value of a variable in the subview it will change it for the whole following document. I think the solution for this problem are environments. The function knitr::knit_child() supports a paramter envir.