klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
833 stars 47 forks source link

Support path as string in `box::use` #261

Closed talegari closed 2 years ago

talegari commented 2 years ago

Please describe your feature request

Firstly, this is a great package and a game changer for R (hoping the community will adopt this on long run).

Request: It would be intuitive to use box::use("dir/file.R") as an alternative for box::use(dir/file.R) (assuming dir is in the box path).

Extending it, here is the syntax suggestion for others:

box::use(
   "purrr",                                                             # 1
   "tbl" = "tibble",                                                # 2
   "dplyr" = list("dplyr" = list("filter", "select")) # 3
    list("stats" = list("st_filter" = "filter", ...))       # 4
)
klmr commented 2 years ago

Thanks for your kind words.

However, unfortunately this feature request is at odds with the underlying design of ‘box’.

In case you’re interested in the technical details: the fact that R allows treating variable names and strings interchangeably (in some contexts) is a major flaw that ‘box’ intentionally doesn’t repeat. Strings are logically distinct from names (the former are dynamic, while the latter are a static part of the code). Treating strings and names identically leads to confusing APIs. It’s an anti-pattern that’s an example of stringly typed programming.

In fact, the predecessor of ‘box’ used strings instead of R expressions, and this turned out to be a mistake. Like Python, and unlike Perl, ‘box’ has an opinionated API rather than providing multiple alternatives of doing the same thing.