Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code organisation in R package development

Tags:

package

r

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

like image 869
Scott Ritchie Avatar asked Sep 03 '13 05:09

Scott Ritchie


People also ask

What are R programming packages?

R packages are extensions to the R statistical programming language. R packages contain code, data, and documentation in a standardised collection format that can be installed by users of R, typically via a centralised software repository such as CRAN (the Comprehensive R Archive Network).

Where are R codes stored?

You put all your R code into the R folder. This folder may or may not have a nested folder structure itself. You probably have a folder for your data and one into which you store all results. The important part here is that you have split your code base into different files.


Video Answer


2 Answers

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.

like image 190
Dirk Eddelbuettel Avatar answered Oct 24 '22 09:10

Dirk Eddelbuettel


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.

like image 24
hadley Avatar answered Oct 24 '22 09:10

hadley