如何避免子调用中的图形文件名

时间:2013-07-07 16:40:32

标签: r knitr

我想在修改数据后重新使用父Rmd中的子文件。该代码似乎工作正常,但第一个数字已经过去,所有数字都被最后一个数字所取代。

有没有办法在每次新呼叫时强制使用新的文件名?

这是我的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'}
```

这个Child.Rmd

XChild

```{r CodeAndFigs}
```

和XCode.R

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

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

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

第一个子呼叫中的数字已被第二个数字替换。我试过玩不同的fig.keep和fig.show选项但没有运气。

1 个答案:

答案 0 :(得分:1)

使用Github上的latest development version(将很快在CRAN上变为knitr 1.3),您可以使用fig.path选项为两个父级中的子文档指定不同的图形路径块CallChildCallChild2,例如

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-'}
```

子文档将从其父块继承选项,因此如果父选项不同,则图形路径不会发生冲突。