Last active
January 24, 2016 06:53
-
-
Save artemklevtsov/c2462f3c8606a3756ace to your computer and use it in GitHub Desktop.
Find an object environment name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getEnvName <- function(f) { | |
attached <- c(environmentName(.GlobalEnv), loadedNamespaces()) | |
envs <- c(.GlobalEnv, lapply(attached[-1], .getNamespace)) | |
attached[vapply(envs, function(env) exists(f, env, inherits = FALSE), logical(1))] | |
} | |
# returns only a first found result | |
getEnvName2 <- function(f) { | |
envs <- c(.GlobalEnv, lapply(loadedNamespaces(), .getNamespace)) | |
for (env in envs) { | |
if (exists(f, env, mode = "function", inherits = FALSE)) | |
return(environmentName(env)) | |
} | |
return(NA) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: