{dir} allows you to store your code in a nested folder structure, rather than solely in the “R” folder, without any code duplication.
Set it up your project with one command and work as usual, just with as many folders as you like.
{dir} won’t be needed by the end users of your package, you don’t even need it in your Suggests dependencies.
Your package won’t violate any CRAN policy. See further below to understand how it’s possible.
Install with:
pak::pak("cynkra/dir")
To set up your package to use it with ‘dir’ we recommend the following :
dir::use_dir_package("any_folder_in_your_repo", "maybe_another", patch = TRUE)
This will set your project’s .RProfile so the multi folder thing just works.
Note that patch = TRUE
means we modify some functions from ‘usethis’
and ‘devtools’. They still work as they did but now support flexible
folder structures. The default patch = FALSE
is less invasive, it
doesn’t touch those but places shims in a "dir-overrides"
environment
on the search path.
For a better experience if you use RStudio we advise that you remap
Ctrl+Shift+D to call devtools::document()
outside of the build pane,
because we can’t patch the build pane and the output of
devtools::document()
is usually terse enough anyway.
For this go to Tools
/ Modify Keyboard Shortcuts
and use the
“Document a package” addin.
dir::use_dir_package()
sets a hook so any time it’s loaded (by you,
not your users) :
- We make sure your additional folders are added to “.Rbuidignore”
- We load the code from your added folders into the
sysdata.rda
file in the R folder - We load the objects into the session
- We patch (if you opt in) the ‘usethis’ and ‘devtools’ function that didn’t play well with the flexible folder structure.
Files and folders starting with “_” or “.” in your added folders will be ignored by the process.
So your code can be outside of the R folder but all the objects are inside, and that makes it CRAN compliant.
Sure, there you go!
This has been raise on r-devel multiple times multiple times but little interest has been shown to implement the feature, so here we are.
These packages implement other solutions to the problem of working with multiple code folders:
- (many)[https://github.com/the-y-company/many] by John Coen (The Y Company)
- (rfold)[https://github.com/feddelegrand7/rfold] by Mohamed El Fodil Ihaddaden packages in