2017-06-26 8 views
2

親コードのチャンク内で子の.Rmdスクリプトを '呼び出す'という問題が発生しています。親RマークダウンファイルのRチャンクは変数間でループし、親からの変数を使用する子Rマークダウンファイルを呼び出します。Knitrと親子のRコードのチャンク:子のチャンクがループ内の親の変数を認識できない

問題:親の変数を通して子値下げファイルになるだけループ Rコードチャンク値下げ中のラテックスコードとして。子のマークダウンファイルのRコードチャンク内にあるものは、ループ内の最初の変数のみを使用します。マークダウンの子供のラテックスコードが正常に動作する原因は何ですか?しかし、Rは親のループ内の変化する変数をフォローしませんか?

親ファイルの理論的な例:

```{r setup, include=FALSE} 
library(knitr) 
knitr::opts_chunk$set(cache=TRUE) 

x <- as.vector(list(1:10)) 
``` 
## Now for the R chunk with loop on the child 
```{r parent} 

xD <- NULL 
for (i in 1:length(x[[1]])){ 
    out = NULL #reset the output of the loop to null, so duplicates aren't printed 
    xD[i] <- as.numeric(x[[1]][i])*2 
    currentValue <- xD[i] 
    out <- c(out, knit_expand(file = "test_child.Rmd")) 
    cat(knit(text=unlist(paste(out, collapse = '\n')), quiet=TRUE)) 
} 

子ファイルtest_child.Rmdの理論的な例:

The value outside a code chunk is `r currentValue` and 
always updates with the variable in the parent's loop. 
```{r childchunk} 
    print(paste0("But inside a code chunk, the value is", currentValue, "and 
    remains the same as the first value, regardless of the parent's loop 
    index.")) ``` 

答えて

4

問題があればcache = TRUE

の使用にダウンしていますチャンクがキャッシュされている場合、コードは実行されていないため、最新の値currentValueは無視されます。

{r childchunk, cache = FALSE} 
    print(paste0("But inside a code chunk, the value is ", currentValue, " and 
    remains the same as the first value, regardless of the parent's loop 
    index. Unless you set cache = FALSE")) 

また、親に注意してください。それをキャッシュしたままにしておくと、子に変更を加えただけでは更新されません。私はあなたが、もちろんただ問題は、実際にキャッシュ設定だった

knitr::opts_chunk$set(cache=FALSE) 
+0

を使用することができます

{r parent, cache = FALSE} 

を使用してお勧めしたいです! – Guy

関連する問題