将代码推迟到Knitr中文档的END [英] Defer code to END of document in knitr

查看:65
本文介绍了将代码推迟到Knitr中文档的END的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用rmarkdown编写报告,然后使用knitr生成pdf.

I am trying to write a report in rmarkdown and then use knitr to generate a pdf.

我希望所有代码都被推送到文档末尾",而只显示与文本交织在一起的结果. echo='hold'选项不执行此操作.

I want all the code to be pushed to the "End of the document", while just displaying results interweaved with my text. The echo='hold' option doesn't do this.

Generate data

```{r chunk1,echo='hold',R.options=}
num_seq<-rnorm(100,0.2)
num_seq
```

We further report the mean of these numbers.  

```{r,echo='hold' }
mean(num_seq)
```

我尝试阅读此处的相关文档 http://yihui.name/knitr/options/,但我不知道该怎么做.

I have tried to read the the relevant documentation found here http://yihui.name/knitr/options/, but I can't figure out how to do this.

推荐答案

我不认为echo='hold'是一个选择.无论如何,诀窍是使用包含代码的echo=FALSE,然后 重新使用相同的块名称 ,并在要使用代码的位置使用eval=FALSE被打印. (两个位置的其他选项都可以,但是这两个是最低要求.)

I don't think echo='hold' is an option. Regardless, the trick is to use echo=FALSE where the code is included, and then re-use the same chunk name and use eval=FALSE where you want the code to be printed. (Other options in both locations are fine, but these two are the minimum required.)

下面的代码评估块所在的代码(并可选地包括代码的输出),但是直到您指定时才包括代码.

The following evaluates the code (and optionally includes output from it) where the chunk is located, but doesn't include the code until you specify.

# Header 1

```{r chunk1, echo=FALSE}
x <- 1
x + 5
```

This is a test.

```{r chunk1, eval=FALSE}
```

导致以下减价:

Header 1
========

    ## [1] 6

This is a test.

    x <- 1
    x + 5

编辑:我经常在具有随机性的R降价文档中经常使用此代码:我从一开始就存储随机种子(无论是手动设置还是仅存储当前随机状态以供以后复制),在附件/附录中显示它:

Edit: I use this frequently in R markdown documents with randomness: I store the random seed in the very beginning (whether I set it manually or just store the current random state for later reproduction) and display it in an annex/appendix:

# Header 1

```{r setseed, echo=FALSE, include=FALSE}
set.seed(seed <- sample(.Machine$integer.max, size=1))
seed
```

This is a test `r seed`.

# Annex A {-}

```{r showsetseed, ref.label='setseed', eval=FALSE}
```

```{r printseed, echo=FALSE}
seed
```

此示例不包含带有原始代码块的结果.不幸的是,结果没有存储,如果以后使用相同的块名称时设置eval=TRUE,它将计算并提供不同的种子.这就是printseed块的原因.我在第一个setseed块中明确显示" seed的原因仅是为了使附件中的showsetseedprintseed块流动良好. (否则,set.seed不会返回数字,因此看起来会很奇怪.)

This example doesn't include the results with the original code chunk. Unfortunately, the results aren't stored, and if I set eval=TRUE when I use the same chunk name later, it will calculate and present a different seed. That's why the printseed block. The reason I explicitly "show" seed in the first setseed block is solely so that, in the annex, the showsetseed and printseed chunks flow well. (Otherwise, set.seed does not return a number, so it would have looked wierd.)

顺便说一句:第二个示例使用ref.label,这是Yihui在此处的更多内容.块重用的通用方法.

BTW: this second example uses ref.label, which Yihui documents here as a more general approach to chunk reuse.

BTW#2:当我说存储随机状态"时,这并不完全正确……我存储的是随机生成的种子.当然,随机状态本身比单个整数大得多.我不想激怒PRNG众神:-)

BTW #2: when I said "store the random state", that's not completely correct ... I'm storing a randomly-generated seed. The random state itself is much larger than a single integer, of course. I don't want to anger the PRNG gods :-)

这篇关于将代码推迟到Knitr中文档的END的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆