import_LL | R Documentation |
The import_LL()
function
places specific functions from a package in the current environment,
and also locks (see lockBinding) the specified functions to prevent modification.
The primary use-case for this function is for exposing functions inside a local environment.
The import_int()
function
directly returns an internal function from a package.
It is similar to the ::: operator, but with 2 key differences:
import_int()
includes the lib.loc
argument.
import_int()
only searches internal functions, not exported ones.
This makes it clearer in your code that you're using an internal function,
instead of making it ambiguous.
import_LL(package, selection, lib.loc = .libPaths())
import_int(form, lib.loc = .libPaths())
package |
a single string,
giving the name of the package to take functions from. |
selection |
a character vector of function names
(both regular functions and infix operators). |
lib.loc |
character vector specifying library search path
(the location of R library trees to search through). |
form |
a two-sided formula, with one term on each side. |
Regarding the Locks in import_LL()
The import_as function returns a locked environment,
just like loadNamespace,
thus protecting the functions from accidental modification or re-assignment.
The import_inops function returns infix operators,
and though these are not locked,
one needs to surround infix operators by back ticks to re-assign or modify them,
which is unlikely to happen on accident.
The import_LL()
function, however, returns "loose" functions.
And these functions
(unless they are infix operators)
do not have the protection due to a locked environment or due to the syntax.
Therefore, to ensure safety from (accidental) modification or re-assignment,
the import_LL()
function locks these functions (see lockBinding).
For consistency, infix operators exposed by import_LL()
are also locked.
Other Details
The import_LL()
and import_int()
functions
do not support importing functions from base/core R.
For import_LL()
:
The specified functions will be placed in the current environment,
and locked.
To unexpose or overwrite the functions, simply remove them; i.e.:
rm(list=c("some_function1", "some_function2")
).
For import_int()
:
The function itself is returned directly.
So one can assign the function directly to some variable, like so:
myfun <- import_int(...)
or use it directly without re-assignment like so:
import_int(...)(...)
tinycodet_import
# Using import_LL ====
import_LL(
"stringi", "stri_sub"
)
# the stri_sub() function now cannot be modified, only used or removed, because it's locked:
bindingIsLocked("stri_sub", environment()) # TRUE
mypaste <- function(x, y) {
import_LL("stringi", selection = "stri_c")
stri_c(x, y)
}
mypaste("hello ", "world")
# Using internal function ====
# Through re-assignment:
fun <- import_int(tinycodet ~ .internal_paste, .libPaths())
fun("hello", "world")
# Or using directly:
import_int(
tinycodet ~ .internal_paste, .libPaths()
)("hello", "world")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.