Title: | Sources an R "Module" with Caching & Encapsulation, Returning Exported Vars |
---|---|
Description: | In the fashion of 'node.js' <https://nodejs.org/>, requires a file, sourcing into the current environment only the variables explicitly specified in the module.exports or exports list variable. If the file was already sourced, the result of the earlier sourcing is returned to the caller. |
Authors: | Rick Wargo <[email protected]> |
Maintainer: | Rick Wargo <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-07 02:55:37 UTC |
Source: | https://github.com/rickwargo/lrequire |
Beware, little error checking is done to see if the indexes are valid. Note the item indexes are 1-based.
append.module.paths(value, after = -1)
append.module.paths(value, after = -1)
value |
(relative) path to insert into |
after |
location in |
Nothing is returned.
# Inserts `../R/lib' as the second path to search for modules append.module.paths('../R/lib', after = 1)
# Inserts `../R/lib' as the second path to search for modules append.module.paths('../R/lib', after = 1)
module
in module.path
.A symbol maybe passed instead of a string for readability. If an expression is passed, it must return a string value.
find.first.R(module, character.only = FALSE, warn.not.found = TRUE)
find.first.R(module, character.only = FALSE, warn.not.found = TRUE)
module |
a string (or symbol) specifying the |
character.only |
a logical value, defaulted to FALSE, that permits an unquoted name to be |
warn.not.found |
a logical value, defaulted to TRUE, can be set to not display warning messages when module is not found. |
A string consisting of the path the module was first found searching through module.paths.
hide.not.found.warnings() # don't warn on files not foudn by find.first.R() # Returns the path to the first found module according to module.paths hello_ex.path <- find.first.R(hello_ex)
hide.not.found.warnings() # don't warn on files not foudn by find.first.R() # Returns the path to the first found module according to module.paths hello_ex.path <- find.first.R(hello_ex)
Returns the current file cache
get.module.cache()
get.module.cache()
environment containing the file cache.
cache <- get.module.cache()
cache <- get.module.cache()
Get existing collection of search paths of where to look for modules.
get.module.paths()
get.module.paths()
Vector of paths (strings) that specify folders where to search for module files, in order.
# Returns a copy of the current module.paths paths <- get.module.paths()
# Returns a copy of the current module.paths paths <- get.module.paths()
This is only to be called when handling results manually. This will be overridden by a warn.not.found
parameter explicitly set by either lrequire
, find.first.R
.
hide.not.found.warnings()
hide.not.found.warnings()
nothing is returned
# Ensure warnings are not displayed when lrequire cannot find the module hide.not.found.warnings()
# Ensure warnings are not displayed when lrequire cannot find the module hide.not.found.warnings()
lrequire
looks in the current path, and then through a list of predefined paths
to search for the given module to source into the current environment, but only making visible
specific variables that are "exported" as a list, in a fashion similar
to node.js. The caching behaviour can be either suspended or it can
re-source files that have changed since the last time the module was cached.
lrequire(module, force.reload = FALSE, character.only = FALSE, warn.not.found = TRUE)
lrequire(module, force.reload = FALSE, character.only = FALSE, warn.not.found = TRUE)
module |
a string (or expression) that specifies a module to load, with or without an optional .R extension. If
the module does not exist in the current directory, it searches for the module in directories listed in
All variables exposed in the module will be hidden in the calling environment, except for what is exposed through module.exports or the exports list variable. |
force.reload |
a logical value, defaulted to FALSE, that can be set to TRUE to disable caching behavior for
the module. If the module has already been loaded and cached, setting |
character.only |
a logical value, defaulted to FALSE, that permits an unquoted name to be |
warn.not.found |
a logical value, defaulted to TRUE, can be set to not display warning messages when module is not found. |
lrequire
operates in a similar principle to modules in node.js - keeping
any variables created in the source module isolated from the calling environment, while exposing a select set
of values/parameters. The specific values are exposed by setting a named list element in the exports
variable
to the desired value or by assigning module.exports
a value.
Note this list exposed in module.exports
should have named items so they can easily be accessed in
the calling environment, however that is not necessary if only a single value is being returned.
If values are assigned to both module.exports
and exports
, only the values in module.exports
will be exposed to the caller.
Caching a long-running operation, such as static data retrieval from a database is a good use of the
caching capability of lrequire
during development when the same module is sourced multiple times.
During development, files can be reloaded, even if being cached, if they have been modified after the time they
were cached. To enable this behaviour, set the variable module.change_code
to 1.
To quickly clear lrequire's package environment, unload the package. In RStudio, this can be done by unchecking
lrequire
on the Packages tab. You can also execute the following at the R prompt:
detach("package:lrequire", unload=TRUE)
The next call to library(lrequire)
will ensure it starts off with a clean slate.
Any values that exist in module.exports
or, if that does not exist, then the
list exports
.
If no module is found, NA
is returned.
Rick Wargo, [email protected]
hide.not.found.warnings() # don't warn on files not found by lrequire() # If the module name is in a character vector, use: my.module <- 'myplot' mm <- lrequire(my.module, character.only = TRUE) say.hello.to <- lrequire(hello_ex) # say.hello.to('Rick') # use the say.hello.to() function that was returned by lrequire()
hide.not.found.warnings() # don't warn on files not found by lrequire() # If the module name is in a character vector, use: my.module <- 'myplot' mm <- lrequire(my.module, character.only = TRUE) say.hello.to <- lrequire(hello_ex) # say.hello.to('Rick') # use the say.hello.to() function that was returned by lrequire()
find.first.R
to find and remove itRemoves module from cache, applying same logic as find.first.R
to find and remove it
remove.from.module.cache(module, character.only = FALSE)
remove.from.module.cache(module, character.only = FALSE)
module |
name of a module, same as the one used in the |
character.only |
a logical value, defaulted to FALSE, that permits an unquoted name to be |
boolean value yielding success of removal from the cache
remove.from.module.cache(variables)
remove.from.module.cache(variables)
module.paths
Beware, little error checking is done to see if the indexes are valid. Note the item indexes are 1-based.
remove.module.paths(index, ...)
remove.module.paths(index, ...)
index |
index of item to be removed from path |
... |
optional indexes of items to be removed from path |
Nothing is returned.
# Removes the2nd and 4th items from module.paths remove.module.paths(2, 4)
# Removes the2nd and 4th items from module.paths remove.module.paths(2, 4)
Note the chace contains other hidden variables, kept in the cache (or environment). These are not removed,
and removing them will conflict with the ability of lrequire
to perform properly.
reset.module.cache()
reset.module.cache()
Nothing is returned
reset.module.cache()
reset.module.cache()
Prints the current file cache
show.module.cache(all.names = FALSE)
show.module.cache(all.names = FALSE)
all.names |
a logical value. If TRUE, all object names are returned. If FALSE, names which begin with a . are omitted. |
Nothing is returned, however, the contents of the module cache are printed to the standard output.
show.module.cache() show.module.cache(all.names = TRUE)
show.module.cache() show.module.cache(all.names = TRUE)
This is the default behaviour.
show.not.found.warnings()
show.not.found.warnings()
This is only to be called when handling results manually. This will be overridden by a warn.not.found
parameter explicitly set by either lrequire
or find.first.R
.
nothing is returned
# Ensure warnings are displayed when lrequire cannot find the module show.not.found.warnings()
# Ensure warnings are displayed when lrequire cannot find the module show.not.found.warnings()