编织,包括来源的R功能文件 [英] Sweave, include sourced R-Function files

查看:97
本文介绍了编织,包括来源的R功能文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个写在文件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功能文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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