Because Go does imports right.
Pallet is a proof of concept of directory-level imports for R.
Important
This package is not battle-tested yet. Use at your own discretion.
A pallet is a directory. Every R script in that directory is a part of the pallet it is in.
If a function is prefixed with a dot (.
), then it is "private" and not available outside the pallet.
However, those dot-prefixed functions can be used in other files within the same pallet.
Functions that aren't prefixed with a dot are "exported" and available outside of the pallet.
Not quite.
With {box} and __init__.R
, you would have to #' @export
everything and re-export within __init__.R
.
However, even then, if you wanted to use something from another file within the same directory, then you'd have to explicitly box::use
in one of the files.
This also creates the risk of someone relying on internals exported for in-directory usage outside of the directory.
With pallet, everything within the directory is available, so there is no risk of exposing internals.
Tip
If you are familiar with {box}, then usage is similar to box:use(path/to/directory)
.
However, with pallet, the entire directory is in use (not only __init__.R
).
With an .R
script in path/to/directory
(its name doesn't matter):
foo <- \() 100
.bar <- \() 23
baz <- \() foo() + .bar()
We can access only the functions that aren't prefixed with dot:
pallet::use("path/to/directory")
directory$foo()
## 100
directory$.bar()
## Error in `$.pallet`(directory, .bar) : name '.bar' not found in 'directory'
directory$baz()
## 123
With pallet you don't have to have everything in a single file.
You can split functions into multiple files and pallet::use()
will make it work for you!
# path/to/directory/file_A.R
foo <- \() 100
.bar <- \() 23
# path/to/directory/file_B.R
baz <- \() foo() + .bar()
The behavior won't change compared to the single file example ✨
pallet::use("path/to/directory")
directory$foo()
## 100
directory$.bar()
## Error in `$.pallet`(directory, .bar) : name '.bar' not found in 'directory'
directory$baz()
## 123
# install.packages("pak")
pak::pak("TymekDev/pallete")
- Single-file pallets
- Multi-file pallets
- Nested pallets (it might work already)
- Disallow pallet package attaching
- Pallet reloading
- Pallet Caching
- Linter for duplicated names within a pallet
I think Go does imports right. Everything within a single directory (package) is available across files. The first letter of an object name determines whether it is exported or not. Uppercase is for exports and lowercase is for private functions.
Personally, I missed the directory-as-one in {box}. I think it is nice to split things across multiple file without exposing them outside the directory.