-
Hi, all: I've scoured the web but haven't seen an explicit answer. What is the difference between box::use and loading a namespace via the double colon operator (i.e., library::namespace)? I was recently introduced to box via the Rhino framework for Shiny development in R. I enjoyed using box so I've started using it for data analysis tasks. However, for such tasks I've traditionally used the double colon operator to reference a namespace and avoid
OR
vs
Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
IMO the convenient thing about If you're asking about performance differences then I can't comment on that. But I'm also of the belief that if you're concerned with performance differences between methods of calling functions then R may not be the best language for you. |
Beta Was this translation helpful? Give feedback.
-
There are two differences: First off, ‘box’ supports loading modules, not just packages. Modules are a modern way of organising and reusing code which is more lightweight and powerful than packages. When organising hierarchical projects in Rhino, you are using modules. Packages have no equivalent functionality. For more details, refer to the Get started vignette. Secondly, when using packages without ‘box’, you basically have to choose between two options:1
‘box’ gives you a lot more choices. For starter, attachment doesn’t happen globally but locally in the current scope. So if a function attaches a package or module, this doesn’t affect other code outside that function. This makes it much less problematic to attach imports. Secondly, you can exert fine-grained control over what names from an import should be attached, unlike box::use(
dplyr[...],
stats[..., filter = stat_filter],
) This attaches all names from ‘dplyr’ as well as ‘stats’, but because the Generally speaking, the most robust way would be if we fully qualified all names all the time (i.e. what your second code does, 1 Nowadays 2 Just compare these two code snippets; I know which one I find more readable: library(dplyr, include.only = c("filter", "mutate", "select"))
library(tidyr, include.only = c("pivot_longer", "pivot_wider")) box::use(
dplyr[filter, mutate, select],
tidyr[pivot_longer, pivot_wider],
) |
Beta Was this translation helpful? Give feedback.
There are two differences:
First off, ‘box’ supports loading modules, not just packages. Modules are a modern way of organising and reusing code which is more lightweight and powerful than packages. When organising hierarchical projects in Rhino, you are using modules. Packages have no equivalent functionality. For more details, refer to the Get started vignette.
Secondly, when using packages without ‘box’, you basically have to choose between two options:1
library(pkgname)
)pkgname::function()
.‘box’ gives you a lot more choices. For starter, attachment doesn’t happen globally but local…