subdirectories in R/ for packages

Python packages can have nested directories for the source code. It appears that is not the case for R, so ALL source code *.R files MUST be in in the ./R/ directory of the package. This leads to long, poorly organized lists of files for large packages (e.g., ggplot2/R at main · tidyverse/ggplot2 · GitHub). Why doesn't R allow for nested directories of source code for packages, as is done for python? Is there any (good) way around this limitation of package development in R?

1 Like

This is a design decision by the authors of R. In theory it could be revisited, but in practice a lot of tooling assumes the current structure, so I doubt that they would do that.

It is possible that you could circumvent it, e.g. by storing the source files in multiple directories within tools/* and then copying them to R/ using a configure script.

However, I suggest that you accept the status quo and not fight the system. Most tools (e.g. test coverage with covr, linting with lintr, styling with styler, etc.) will not work, unless you keep all source files in a single directory.

2 Likes

Thanks @Gabor for the suggestions. Naively, it seems like re-tooling to add recursive search into subdirectories of R/ wouldn't been so much work, especially relative to the opposite. Do you know the reason why the authors of R chose to not allow a subdirectory structure in the R/ directory?

It would be helpful if this feature/limitation of R package development is clearly stated in the docs on developing R packages (e.g., https://r-pkgs.org/). I haven't seen any docs clearly stating that one CANNOT create a subdirectory structure in R/ for packages. It seems that a new R package developer must just figure this out by trying and failing to create a subdirectory structure in R/ for their R package.

I am pretty sure that it would break a lot of tools, IDEs like RStudio, the R mode of VS Code, devtools, etc. Additionally, a package that has R code in subdirectories would only work with the next version of R, which is not great for users.

I don't know why.

There are some packages like {box} that allow a more modular approach with nested directories: GitHub - klmr/box: Write reusable, composable and modular R code

I have never personally used it but it seems reasonably popular on GitHub.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.