R knitr在显示之前执行代码

时间:2015-11-04 17:49:36

标签: r knitr r-markdown

我是R的knitr的新手并且喜欢它。我正在努力解决两个问题。

我想执行一个代码块(echo = F),谈谈操作的结果(可用数字),然后显示代码块。我可以使用eval / echo切换来实现这一点,但在我谈论它之前和之后必须复制代码。哪个好,除非我必须修改代码,然后我需要在两个地方更改它。

还有其他办法吗?

其次,我想做上面的事情,然后包括代码,减去中间的所有文本作为附录。看看现在出现的类似问题,看起来我需要查找一下。有什么想法吗?

谢谢, 鲍勃

1 个答案:

答案 0 :(得分: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中引用它们文档。