# Set the module search path for the example module.
old_opts = options(box.path = system.file(package = 'box'))
# Basic usage
# The file `mod/hello_world.r` exports the functions `hello` and `bye`.
box::use(mod/hello_world)
hello_world$hello('Robert')
hello_world$bye('Robert')
# Using an alias
box::use(world = mod/hello_world)
world$hello('John')
# Attaching exported names
box::use(mod/hello_world[hello])
hello('Jenny')
# Exported but not attached, thus access fails:
try(bye('Jenny'))
# Attach everything, give `hello` an alias:
box::use(mod/hello_world[hi = hello, ...])
hi('Eve')
bye('Eve')
# Reset the module search path
on.exit(options(old_opts))
if (FALSE) {
# The following code illustrates different import declaration syntaxes
# inside a single `box::use` declaration:
box::use(
global/mod,
mod2 = ./local/mod,
purrr,
tbl = tibble,
dplyr = dplyr[filter, select],
stats[st_filter = filter, ...],
)
# This declaration makes the following names available in the caller’s scope:
#
# 1. `mod`, which refers to the module environment for `global/mod`
# 2. `mod2`, which refers to the module environment for `./local/mod`
# 3. `purrr`, which refers to the package environment for ‘purrr’
# 4. `tbl`, which refers to the package environment for ‘tibble’
# 5. `dplyr`, which refers to the package environment for ‘dplyr’
# 6. `filter` and `select`, which refer to the names exported by ‘dplyr’
# 7. `st_filter`, which refers to `stats::filter`
# 8. all other exported names from the ‘stats’ package
}
Run the code above in your browser using DataLab