使knitr运行ar脚本:我使用read_chunk还是source?

Far*_*rel 19 r rstudio knitr

我正在使用RStudio版本0.97.312运行R版本2.15.3.我有一个脚本从各种来源读取我的数据并创建几个data.tables.然后我有另一个r脚本,它使用在第一个脚本中创建的data.tables.我想将第二个脚本转换为R降价脚本,以便分析结果可以作为报告输出.

我不知道的目的read_chunk,而不是source.我read_chunk没有工作,但source正在工作.无论哪种情况,我都无法在RStudio的工作区面板中看到对象.

请解释之间的差异read_chunksource?我为什么要使用其中一个?为什么我的.Rmd脚本不起作用

这是一个荒谬简化的样本

这是行不通的.我收到以下消息

错误:找不到对象'z'

两个简单的文件......

测试源到rmd.R

x <- 1:10
y <- 3:4
z <- x*y  
Run Code Online (Sandbox Code Playgroud)

测试源.Rmd

Can I run another script from Rmd
========================================================

Testing if I can run "test of source to rmd.R"

```{r first part}
require(knitr)
read_chunk("test of source to rmd.R")
a <- z-1000
a
```

The above worked only if I replaced "read_chunk" with "source". I 
can use the vectors outside of the code chunk as in inline usage. 
So here I will tell you that the first number is `r a[1]`. The most 
interesting thing is that I cannot see the variables in RStudio 
workspace but it must be there somewhere.
Run Code Online (Sandbox Code Playgroud)

Yih*_*Xie 13

read_chunk()只读取源代码(以供将来参考); 它没有评估代码source().本页以及手册中read_chunk()解释了目的.

  • 我读了"代码外化".也许是因为我不是一个"真正的"程序员,我起初并不欣赏阅读和评估之间的区别.为什么要读取以前的代码但不评估它?对于我想做的事情,我可以看到我显然必须使用`source`. (5认同)
  • 我还了解到每个knitr run都是一个单独的会话,不能使用我的RStudio会话中存在的对象.因此,当我编写(起草,开发)我的.Rmd脚本时,我需要先简单地插入由原始源.r文件生成的.Rdata映像文件.如果我在开发过程中反复运行我的.Rmd来看看我是怎么做的,那么每次执行`source`行时我都要等待10-20秒.一旦我完成所有工作,我就可以恢复使用`source`,以便将来的运行拉入最新数据. (2认同)