28

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/.

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/.

Is it possible to organise code in subfolders when developing a package, and if so, how? The Writing R Extensions manual doesn't offer any guidance, nor does R CMD build detect files stored in subfolders under R/.

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64

4 Answers4

10

You can't use subfolders without additional setup (like defining a custom makefile). The best you can do is to use prefixes: client-a.r, client-b.r, server-a.r, server-b.r, etc.

hadley
  • 102,019
  • 32
  • 183
  • 245
  • 5
    **But of course you can**. If you know what you are doing, and are willing to do the legwork of correct `src/Makevars`. The prime example may be the [Matrix](http://cran.r-project.org/package=Matrix) package which has five (5) subfolders below `src/`, and some of those include other folders. Now, would this be the recommended approach. No, but it a) feasible and b) being done. – Dirk Eddelbuettel Sep 03 '13 at 13:59
  • 5
    @DirkEddelbuettel Of course you can do anything if you try hard enough. You could even store all the code in a database. – hadley Sep 03 '13 at 14:01
  • No you cannot as CRAN will not accept a database. But it does accept packages with subfolders. – Dirk Eddelbuettel Sep 03 '13 at 14:01
  • 3
    Can you please stop being silly. Quote me the line or paragraph in "Writing R Extensions" which a) suggests use of a database to store files and b) prohibits use of subfolders to `src/`. – Dirk Eddelbuettel Sep 03 '13 at 14:05
  • 2
    @DirkEddelbuettel R-ext doesn't prohibit the use of subfolders, but it say they're supported either. It also doesn't prohibit the use of databases, and in fact tells you that your code will be stored in a database. Regardless, I've tweaked my answer. But I don't think you can use makefiles in your `R/` directory. – hadley Sep 03 '13 at 14:13
  • 2
    I explicitly refered to Makevars, not Makefile. Please do not attribute incorrect statements to me. – Dirk Eddelbuettel Sep 03 '13 at 14:15
10

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. Other example is the Rsymphony packages (co-)written and maintained by an R Core member.

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. 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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 3
    you can put subfolders below `src/`, but can you put them below `R/`? Seems to me that's a slight but important distinction ... – Ben Bolker Sep 03 '13 at 16:16
  • For `R/` you cannot; they get collated / parsed anyway and stored in an internal format. For `src/` you can, with some (considerable) pain. – Dirk Eddelbuettel Sep 03 '13 at 16:27
  • 1
    Just adding that storing code in subfolders to `R/` and manually adding the files to the `Collate:` field in the `DESCRIPTION` file doesn't work either. – Scott Ritchie Sep 03 '13 at 22:38
  • 1
    Your and @hadley’s answers together with the following clarification would be a complete, useful answer: “R handles subfolders for code organization only for C/C++ files in `src/` with some (TODO: which‽) `Makevars` set, but not at all for R files in `R/`.” – flying sheep Feb 10 '17 at 10:27
  • No, that would not be a useful answer. – Dirk Eddelbuettel Feb 10 '17 at 12:22
3

I argued with R core team Allow for sub-folders in "package/R/" directory . 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

R/mcmc/a.R R/mcmc/b.R R/prediction/p1.R R/predection/p2.R

2) When I need to pack them, I convert all files under R/ as

R/mcmc_a.R R/mcmc_b.R R/prediction_p1.R R/predection_p2.R ... with my package.flatten() function

3) Then I install the flattened version to R.

I wrote a simple script for Linux to do everything

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

Feng
  • 101
  • 1
  • 7
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.

Joel Graff
  • 167
  • 15