[英]How can I use get0 in my R package to search only within package namespace?
Let's say I have an internal function in my package, call it is_()
, which can be thought of as a more generalized version if is()
.假设我的 package 中有一个内部 function,称之为 is_
is_()
,如果is()
可以将其视为更通用的版本。 In particular, it searches for specialized functions that are used to tell whether the supplied object is the given class.特别是,它搜索用于判断提供的 object 是否是给定的 class 的专用函数。 For example,
is_()
might be programmed as:例如,
is_()
可能被编程为:
is_ <- function(obj, class) {
if (exists(paste0("is.", class))) {
get0(paste0("is.", class))(obj)
}
else inherits(obj, class)
}
That way, if I'm doing argument checking in my package, I can run something like这样,如果我在我的 package 中进行参数检查,我可以运行类似
is_(x, "numeric_vector")
where I've defined我定义的地方
is.numeric_vector <- function(x) is.numeric(x) && is.null(dim(x))
within my package.在我的 package 中。
A problem arises when is.numeric_vector()
is defined outside my package, eg, by another package the user has loaded.当
is.numeric_vector()
在我的 package 之外定义时出现问题,例如,由用户加载的另一个 package 定义。 In that case, exists()
and get0()
both find the function wherever they can, but I want to restrict the search to function defined in my package and included in my package's namespace (ie, all imported packages).在这种情况下,
exists()
和get0()
都尽可能地找到 function,但我想将搜索限制在我的 ZEFE90A8E604A7C840E88D03A67F6B7 中定义的 function 中(即所有导入的包都包含在我的包中)。 The envir
and inherits
arguments seem to get at what I want, but I don't know how to supply them to get the result I want. envir
和inherits
arguments 似乎得到了我想要的,但我不知道如何提供它们以获得我想要的结果。 How can I restrict get0()
to only search for its argument within my package's namespace?如何将
get0()
限制为仅在包的命名空间中搜索其参数?
The problem is that your package namespace will inherit from the base namespace which inherits from the global environment.问题是您的 package 命名空间将从全局环境继承的基本命名空间继承。 For a more detailed explanation, see here: https://adv-r.hadley.nz/environments.html#namespaces .
有关更详细的说明,请参见此处: https://adv-r.hadley.nz/environments.html#namespaces 。 If you want more control over the symbol look up, you'll need to do the work yourself.
如果您想更好地控制符号查找,您需要自己完成这项工作。 You could include your own
get
function in your package您可以在 package 中包含您自己的
get
function
# These are private helpers that do not need to be exported from your package.
.pkgenv <- environment()
get1 <- function(name, env = .pkgenv) {
if (identical(env, emptyenv())) {
NULL
} else if (identical(env, globalenv())) {
# stop at global env
NULL
} else if (exists(name, envir=env, inherits = FALSE)) {
env[[name]]
} else {
# try parent
get1(name, parent.env(env))
}
}
This will recursively search for the symbol in environments but stops at the global environment.这将在环境中递归搜索符号,但在全局环境中停止。 You could use it with your
is_
function like您可以将它与您的
is_
function 一起使用
is_ <- function(obj, class) {
if (!is.null(fn <- get1(paste0("is.", class)))) {
fn(obj)
} else {
inherits(obj, class)
}
}
Here we just check for null rather than separately verifying the name and then retrieving the value.这里我们只检查 null 而不是单独验证名称然后检索值。 If
get1
is something that will be called a bunch you might want to consider caching the result so you don't always have to walk the inheritance tree.如果
get1
将被称为一堆,您可能需要考虑缓存结果,这样您就不必总是遍历 inheritance 树。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.