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.

1
2
3
4
5
6
7
8
9
---
output: html_document
params:
  data: NA
---
 
```{r}
summary(params$data)
```

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
---
title: "main"
author: "rstats-tips"
date: "22. Januar 2016"
output: html_document
---
 
# A
```{r, echo=FALSE}
 params=list(data=cars)
```
```{r child='subview.Rmd'}
```
 
# B
```{r, echo=FALSE}
 params=list(data=iris)
```
```{r child='subview.Rmd'}
```

The name of this file is Main Report.Rmd

The result is this:

Output of Main Report.Rmd

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.