在R
中开发包时,所有R
源文件都放在子目录R/
中,所有已编译的代码都放在子目录src/
中。
我想将一些组织添加到这些文件夹中的文件,而不是将所有内容转储到顶层。例如,假设我假设开发了一个客户端 - 服务器应用程序。从逻辑上讲,我想在R/client/
和R/server/
中的所有服务器R源文件中组织我的所有客户端R源文件。
在开发包时是否可以在子文件夹中组织代码,如果是,如何? Writing R Extensions手册未提供任何指导,R CMD build
也未检测R/
下的子文件夹中存储的文件。
答案 0 :(得分:10)
将评论扩展到Hadley的IMHO错误答案:
查看Matrix包(由R Core成员编写),其中包含五个文件夹src/
,其中两个包含其他子文件夹。其他示例是由R Core成员编写和维护的Rsymphony包(co-)。
这样做不适合胆小的人。 R 强烈更喜欢整个src/Makevars
上的src/Makefile
片段,以便能够为不同的子体系结构构建自己的Makefile
版本。但是,如果你知道一点点制作并且愿意投入精力,这完全可行 - 并且已经完成了。
但仍然没有推荐它。
答案 1 :(得分:7)
如果没有其他设置(如定义自定义makefile),则无法使用子文件夹。您可以做的最好的事情是使用前缀:client-a.r
,client-b.r
,server-a.r
,server-b.r
等。
答案 2 :(得分:1)
Recognizing the thread is a bit old, I just thought I'd throw in my solution to this problem. Note that my issue is similar, but I am only concerned with preserving folder hierarchies in development.
In development, I organize my script files in subfolders to my heart's content, but rather than fight R's flat hierarchy in production, I added my own "compile-time constant", so to speak.
That is, in every file located in a subfolder (not in top-level scripts/), I add the following:
if (!exists("script.debug"))
script.debug = FALSE
Then, I load whatever other dependencies are required as follows:
source.list <- c(
"script_1.R",
"script_2.R",
"script_3.R",
"script_4.R"
)
if (script.debug)
source.list <- paste("./script_subfolder/", source.list, sep="")
lapply(source.list, source)
The default assumption is that the code is in production, (source.debug = FALSE), so when in development, just ensure that source.debug = TRUE and the project's script/ folder is set as the working directory before loading any script files.
Of course, this example's a bit simple - it assumes that all script file dependencies exist in the same folder, but it seems a simple issue to devise a system that would suit more complicated development folder hierarchies.
答案 3 :(得分:0)
我与R核心团队Allow for sub-folders in "package/R/" directory争论。他们似乎并不想改善它。所以我的工作流程如下。
1)创建一个与其他包相同的R项目,但允许文件夹R/
中的子目录,例如
R/mcmc/a.R
R/mcmc/b.R
R/prediction/p1.R
R/predection/p2.R
2)当我需要打包它们时,我将R/
下的所有文件转换为
R/mcmc_a.R
R/mcmc_b.R
R/prediction_p1.R
R/predection_p2.R
...
使用我的package.flatten()
功能
3)然后我将扁平版本安装到R。
我写了一个简单的Linux脚本来做所有事情
https://github.com/feng-li/flutils/blob/master/inst/bin/install.HS