Skip to content

Feature request: recursive import::into/from when .into is an environment (but not environment()) #101

Description

@mmuurr

I'm aware of #34, but I'm curious if there's any appetite for allowing import::into() and import::from() to work recursively when .into is supplied and (i) is an environment and (ii) not environment(). The use case I have in mind is when we capture the imports into a 'module-like' object, rather than attaching individual module contents into the current frame. So, e.g.,:

utils.R:

log <- function(...) cat(sprintf(...), sep = "\n")

na2 <- function(x, replacement = NA) {
  x[is.na(x)] <- replacement
  x
}

maths.R:

utils <- import::from("utils.R", .all = TRUE, .into = new.env())

square <- function(x, .na = NA) {
  utils$na2(x, .na)^2
}

Now at the console:

maths <- import::from("maths.R", .all = TRUE, .into = new.env())
ls(maths)
# [1] "log"        "na2"        "square"     "triangular" "utils"

... utils, log, and na2 are all in the environment, but the intent was to keep the contents of utils.R in the local utils environment.

It's definitely true that if utils.R happened to also include a call to import::from() and not specify a capturing environment this could cause great confusion, but I think the 'strict' case shown above mimics the patterns used by Python, JS, etc.

Perhaps extending the set of commands (here, from, into) with something like import::env(...) that only ever imports into a new.env() (that typically would then be captured as a variable for downstream usage).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions