简体   繁体   English

R package开发中的代码组织

[英]Code organisation in R package development

When developing packages in R all R source files are put in the subdirectory R/ , and all compiled code is put in the subdirectory src/ .R中开发包时,所有R源文件都放在子目录R/中,所有编译后的代码都放在子目录src/中。

I would like to add some organisation to files within these folders, rather than have everything dumped at the top level.我想为这些文件夹中的文件添加一些组织,而不是将所有内容都转储在顶层。 For example, lets say I'm hypothetically developing a client-server application.例如,假设我正在开发一个客户端-服务器应用程序。 Logically, I would like to organise all my client R source files in R/client/ and all my server R source files in R/server/ .从逻辑上讲,我想在R/client/中组织我的所有客户端 R 源文件和在R/server/中的所有服务器 R 源文件。

Is it possible to organise code in subfolders when developing a package, and if so, how?开发 package 时是否可以在子文件夹中组织代码,如果可以,如何? The Writing R Extensions manual doesn't offer any guidance, nor does R CMD build detect files stored in subfolders under R/ .编写 R 扩展手册不提供任何指导, R CMD build检测存储在R/下的子文件夹中的文件。

Expanding the comment to Hadley's IMHO incorrect answer: 将评论扩大到哈德利的恕我直言,错误答案:

Look at the Matrix package (written by R Core members) which has five folders below src/ , and two of these contain other subfolders. 查看Matrix包(由R Core成员编写),它在src/下面有五个文件夹,其中两个包含其他子文件夹。 Other example is the Rsymphony packages (co-)written and maintained by an R Core member. 其他示例是由R Core成员编写和维护的Rsymphony包(co-)。

Doing this is not for the faint of heart. 这样做不适合胆小的人。 R strongly prefers a src/Makevars fragment over a full src/Makefile in order to be able to construct its own Makefile versions for the different subarchitectures. R 强烈倾向于在完整的src/Makefile使用src/Makevars片段,以便能够为不同的子体系结构构建自己的Makefile版本。 But if you know a little make and are willing to put the effort in, this is entirely doable -- and being done. 但是如果你知道一点点制作并愿意付出努力,这完全是可行的 - 并且已经完成了。

That still does not make it recommended though. 尽管如此,仍然没有建议。

You can't use subfolders without additional setup (like defining a custom makefile). 没有其他设置(如定义自定义makefile),您无法使用子文件夹。 The best you can do is to use prefixes: client-ar , client-br , server-ar , server-br , etc. 您可以做的最好是使用前缀: client-arclient-brserver-arserver-br等。

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. 在开发过程中,我将子文件夹中的脚本文件组织到我心中的内容,但是我没有在生产中对抗R的扁平层次结构,而是添加了我自己的“编译时常量”,可以这么说。

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. 默认假设是代码处于生产状态(source.debug = FALSE),因此在开发时,只需确保source.debug = TRUE,并在加载任何脚本文件之前将项目的脚本/文件夹设置为工作目录。

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. 当然,这个例子有点简单 - 它假设所有脚本文件依赖存在于同一个文件夹中,但设计一个适合更复杂的开发文件夹层次结构的系统似乎是一个简单的问题。

I argued with R core team Allow for sub-folders in "package/R/" directory . 我与R核心团队争论允许“package / R /”目录中的子文件夹 They seem not really want improve it. 他们似乎并不想改善它。 So my workflow is as follows. 所以我的工作流程如下。

1) Create an R project same as other packages but allow sub-directories in folder R/ such as 1)创建一个与其他包相同的R项目,但允许文件夹R/的子目录,例如

R/mcmc/aR R/mcmc/bR R/prediction/p1.RR/predection/p2.R

2) When I need to pack them, I convert all files under R/ as 2)当我需要打包它们时,我将所有文件转换为R/ as

R/mcmc_a.RR/mcmc_b.RR/prediction_p1.RR/predection_p2.R ... with my package.flatten() function R/mcmc_a.RR/mcmc_b.RR/prediction_p1.RR/predection_p2.R ...使用我的package.flatten()函数

3) Then I install the flattened version to R. 3)然后我将扁平版本安装到R.

I wrote a simple script for Linux to do everything 我为Linux编写了一个简单的脚本来完成所有工作

https://github.com/feng-li/flutils/blob/master/inst/bin/install.HS https://github.com/feng-li/flutils/blob/master/inst/bin/install.HS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM