2015-11-04 82 views
2

我是R中的knitr,並且很喜歡它。有兩件事我正在努力。R knitr在顯示之前執行代碼

我想執行代碼塊(echo = F),討論操作的結果(帶有可用的數字),然後顯示代碼塊。我可以使用eval/echo切換來實現這一點,但是在我談論它之前和之後必須重複代碼。除了當我需要修改代碼時,這是很好的,然後我需要在兩個地方改變它。

有沒有另一種方法呢?其次,我想做一下上面的代碼,然後在代碼中加入代碼,將中間的所有文本作爲附錄減去。看着現在出現的類似問題,看起來我需要查找purl。有什麼想法嗎?

感謝, 鮑勃

回答

1

你可以用ref.label參數做到這一點。

  
```{r chunk1, echo = FALSE} 
summary(cars) 
``` 

Here I am talking about `chunk1` even though we did not 
see the code that generated its results. 

Let's look at the code that made the output above... 


```{r chunk2, ref.label = `chunk1`, eval = FALSE} 
# Nothing here, not going to evaluate... 
``` 

Now we see the code that generated `chunk1` even though I 
did not re-evaluate it nor did I have to copy it. 

針織這HTML應該產生:

Rendered HTML

注:沒有理由來命名塊chunk1chunk2等,您可以命名它們無論你想和在整個.Rmd文檔中參考它們。

+0

甜,我錯過了ref.label的意義。這正是我想要的。我想我可以這樣做來創建代碼附錄。 –

+0

更多信息請登錄http://yihui.name/knitr/demo/reference/ –

相關問題