ap53
ap53

Reputation: 823

How to avoid figure filenames in child calls

I would like to reuse a child file from my parent Rmd after modifying my data. The code seems to work fine, but the first figures are stepped over and all figures are replaced by the last one.

Is there a way to force new filenames with each new call?

This is my Parent.Rmd

XParent  
========  
```{r Opts, echo=FALSE}
opts_chunk$set(fig.show='asis', fig.keep='all', fig.width=3, fig.height=4, options(digits = 2), dev='jpeg')
```  
```{r XLoad}
read_chunk(lines = readLines('XCode.R'))
``` 

```{r ParentChunk}
```

First child call
---------------
#### NOTICE the data is OK but the figure corresponds to the second child call (Y axis = 1200) 
```{r CallChild, child='XChild.Rmd'}
```


#### I now modify the dataframe
```{r}
df$dist <- df$dist * 10
```

Second child call
-----------------
As this is the last case, the figure agrees with the data:
```{r CallChild2, child='XChild.Rmd'}
```

This Child.Rmd

XChild

```{r CodeAndFigs}
```

and XCode.R

## @knitr ParentChunk
df <- cars
colMeans(df)

# Y axis' upper limit is 120
plot(cars)

## @knitr CodeAndFigs
colMeans(df)
plot(df)

The figure in the first child call has been replace by the second figure. I have tried playing with different fig.keep and fig.show options with no luck.

Upvotes: 1

Views: 227

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30194

With the latest development version on Github (which will turn into knitr 1.3 very soon on CRAN), you can use the fig.path option to specify different figure paths for the child document in two parent chunks CallChild and CallChild2, e.g.

XParent  
========  
```{r Opts, echo=FALSE}
opts_chunk$set(fig.show='asis', fig.keep='all', fig.width=3, fig.height=4, options(digits = 2), dev='jpeg')
```  
```{r XLoad}
read_chunk(lines = readLines('XCode.R'))
``` 

```{r ParentChunk}
```

First child call
---------------
#### NOTICE the data is OK but the figure corresponds to the second child call (Y axis = 1200) 
```{r CallChild, child='XChild.Rmd', fig.path='figure/child-'}
```


#### I now modify the dataframe
```{r}
df$dist <- df$dist * 10
```

Second child call
-----------------
As this is the last case, the figure agrees with the data:
```{r CallChild2, child='XChild.Rmd', fig.path='figure/child2-'}
```

The child document will inherit options from its parent chunk, so the figure paths will no clash if the parent options are different.

Upvotes: 1

Related Questions