Home > Software engineering >  Creating a rmarkdown document based on another rmarkdown document
Creating a rmarkdown document based on another rmarkdown document

Time:07-26

I am in need of your help to, given a rmarkdown document as the one below, create a second rmarkdown document, according to the rules enumerated in it.

Notice, however, that such rules are not supposed to be in the document: that is only to describe to you what I am wanting to obtain.

---
output: pdf_document
params:
  sec1: 1
---

## section 1

```{r a}
x <- 2
y <- 3

x y
```


```{r b}
x <- 4
y <- 5

x y
```

Here should be the code of {r a} or the code of {r b}, 
if params$sec1 == 0 and if params$sec1 == 1, respectively. 

### A

The result of {r a} if params$sec1 == 0. 
The result of {r b} if params$sec1 == 1. 

### B

The result of {r b} if params$sec1 == 0. 
The result of {r a} if params$sec1 == 1. 

The expected output, when params$sec1 == 1:

---
output: pdf_document
---

## section 1

x <- 4
y <- 5

x y

### A

9.

### B

5.

CodePudding user response:

Here's another way to do it (which is different from my first answer so I prefer writing another one).

Basically, you can create booleans at the beginning on the file, and then use them to conditionally echo and eval each chunk:

---
output: pdf_document
params:
  sec1: 1
---

```{r echo = FALSE}
sec1_is_0 <- params$sec1 == 0
sec1_is_1 <- params$sec1 == 1
```


## section 1

```{r a, echo = sec1_is_0, eval = FALSE}
x <- 2
y <- 3

x y
```


```{r b, echo = sec1_is_1, eval = FALSE}
x <- 4
y <- 5

x y
```


### A

```{r a, echo = FALSE, eval = sec1_is_0}
```

```{r b, echo = FALSE, eval = sec1_is_1}
```


### B

```{r b, echo = FALSE, eval = sec1_is_0}
```

```{r a, echo = FALSE, eval = sec1_is_1}
```

CodePudding user response:

One way to do it is to conditionally wrap chunks in \begin{comment} and \end{comment} based on the parameter value. It works for a small example like this but I think it will become tedious and hard to read in longer documents (and also if your parameter has more than 2 possible values):

---
output: pdf_document
header-includes:
  - \usepackage{comment}
params:
  sec1: 1
---

## section 1

`r if(params$sec1 == 1) {"\\begin{comment}"}`
```{r a, echo = TRUE, eval = FALSE}
x <- 2
y <- 3

x y
```
`r if(params$sec1 == 1) {"\\end{comment}"}`


`r if(params$sec1 == 0) {"\\begin{comment}"}`
```{r b, echo = TRUE, eval = FALSE}
x <- 4
y <- 5

x y
```
`r if(params$sec1 == 0) {"\\end{comment}"}`


### A

`r if(params$sec1 == 1) {"\\begin{comment}"}`
```{r a, echo = FALSE, eval = TRUE}
```
`r if(params$sec1 == 1) {"\\end{comment}"}`

`r if(params$sec1 == 0) {"\\begin{comment}"}`
```{r b, echo = FALSE, eval = TRUE}
```
`r if(params$sec1 == 0) {"\\end{comment}"}`


### B

`r if(params$sec1 == 1) {"\\begin{comment}"}`
```{r b, echo = FALSE, eval = TRUE}
```
`r if(params$sec1 == 1) {"\\end{comment}"}`



`r if(params$sec1 == 0) {"\\begin{comment}"}`
```{r a, echo = FALSE, eval = TRUE}
```
`r if(params$sec1 == 0) {"\\end{comment}"}`

With params$sec1 == 1:

enter image description here

With params$sec1 == 0:

enter image description here

  • Related