问题描述
我有一个写在文件a.R中的Rjob,它调用了写在文件b.R中的函数,以及一个snw文件c.snw.我通过"R CMD c.snw"调用c.snw.
I have an Rjob writen in file a.R which calls functions writen in file b.R, and a snw file c.snw. I call c.snw via "R CMD c.snw".
我正在寻找仅通过\ SweaveInput {a.R}将a.R包含到c.snw中的可能性,但是从a.R调用的函数的代码也应该写在tex文件中.这意味着Sweave会跟踪包含的主R文件(此处为a.R)中的所有source()命令.
I am looking for a possibilty to only include a.R via \SweaveInput{a.R} into c.snw, but the code of the functions called from a.R should also be written in the tex file. This implies that Sweave traces all source() commands in the included main R file (here a.R).
有人可以建议如何做吗?
Can anyone suggest how to do this?
推荐答案
在这种情况下,我想使用knitr
包会使生活变得更轻松.您可以将a.R
作为块label-a
读取,并将其写入c.Rnw
:
I guess life will be easier with the knitr
package in this case. You can read a.R
as a chunk label-a
, and write it in c.Rnw
:
<<read-code>>=
read_chunk('a.R', labels = 'label-a')
@
<<label-a>>
@
当您使用library(knitr); knit('c.Rnw')
编译c.Rnw
时,结果中将包含a.R
(等同于将代码复制并粘贴到块label-a
中).在knitr
中称为代码外部化.
When you compile c.Rnw
with library(knitr); knit('c.Rnw')
, a.R
will be included into the results (it is equivalent to copy & paste code into the chunk label-a
). This is called code externalization in knitr
.
\SweaveInput{}
不用于输入R源代码;您只能输入Rnw文档.
\SweaveInput{}
is not used to input R source code; you can only input Rnw documents.
这篇关于编织,包括来源的R功能文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!