{dir} allows you to store your code in a nested folder structure, rather than solely in the “R” folder, without any code duplication.
It’s useful if you have a lot of scripts or if you want to turn a non package project into a package.
Set 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 :
# allow nested folders under R/ and patch useful workflow functions
dir::use_dir_package(patch = TRUE)
# use additional folders
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 in the scope of this project
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 (but these
won’t be triggered by your existent key bindings).
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 rather than “Build Package Documentation”.
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, here’s a minimal example where you can inspect the file structure, NAMESPACE, .RProfile, .Rbuildignore etc : https://github.com/cynkra/dir/tree/main/inst/dir.example
This has been raised on r-devel 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