1

修改数据后,我想重用父 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'}
```

这个孩子.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 选项,但没有成功。

4

1 回答 1

1

使用 Github 上的最新开发版本(将knitr 1.3很快在 CRAN 上变为),您可以使用该选项为两个父块中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-'}
```

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

于 2013-07-15T05:36:44.970 回答