Load the library into an existing environment (equivalent to the "local" parameter "source")?

I use functions in production in an environment to encapsulate (and group) auxiliary functions:

Helper File:

# File: Helper.R hello <- function() { print("Hello world") } 

Customer:

 helper <- new.env() source("Helper.R", local=helper) helper$hello() # call the helper function 

How can I transfer my "Helper.R" source to the library without breaking the calls to the original functions?

I want something like

 helper <- new.env() library(Helper, local=helper) helper$hello() # call the helper function (loaded from the library now) 

Is there any way to do this?

+1
source share
4 answers

You can use the import_package function in the package (and not the one on CRAN, its different!).

Then the following locally installs the package:

 modules::import_packge('pkg', attach = TRUE) 

Alternatively and potentially closer to what you really want to do, you can use it like this:

 pgk = modules::import_package('pkg') 

Now the package is not connected at all, and its exported objects can be accessed via pkg$obj . This is somewhat similar to the basic Rs loadNamespace function, but significantly more behind the scenes.

Finally, consider not putting your helper code in a package at all, but rather distributing it as a module. This is still what the package was designed for. So instead of creating a package, just distribute your helper.r file (or folder), and then use it like this:

 helper = modules::import('helper') 

See README and vignette for a detailed description.

+1
source

When adding to the list of offers, you can also consider using the modules package on CRAN (note that I am the author). When you have a Helper.R file with:

 hello <- function() { print("Hello world") } 

you can use

 helper <- modules::use("Helper.R") helper$hello() 

to encapsulate your helper functions in their own environment. The package also provides some functions for managing the local module namespace (import / export).

+1
source

Another way:

 # getNamespace returns the environment representing the name space name. The namespace is loaded if necessary. # Internal function to support reflection on namespace objects! env <- getNamespace("data.table") cars <- env$as.data.table(mtcars) 

In this example, all objects in the data.table package data.table accessible through the env environment variable.

Note. It uses the internal function R (no matter how dangerous the risk of change is).

0
source

I found another package called import (similar to "modules") that allows you to import packages into the environment:

https://github.com/smbache/import

This package is also included in CRAN:

 install.packages("import") 

It allows you to import selected, all exported ("publicly available") or all (not even exported) package functions.

Example:

 import::from(dplyr, arrange, .into = "datatools") 

The import::from function is a convenient wrapper around getExportedValue .

0
source

Source: https://habr.com/ru/post/1245201/