问题描述
我在文件 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
读成一个chunk 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) 编译
, c.Rnw
时;knit('c.Rnw')aR
将包含在结果中(相当于将代码复制 & 粘贴到块 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.
这篇关于Sweave,包括源 R-Function 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!