简体   繁体   English

如何在我的 R package 中使用 get0 仅在 package 命名空间内搜索?

[英]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. envirinherits 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.

相关问题 R:如何让我的包使用另一个包? - R: How do I make my package use another package? 如何获得R(使用哪个程序包)中的重新捕获概率? - How can I get the recapture probabilities in R (which package to use) ? R我可以在包中使用.rds文件作为我的数据吗? - R Can I use .rds files for my data in a package? 如何在 R 包中的函数内使用数据? - How to use data within a function in an R package? 有没有可以用来在R中获得目标结果规则的软件包? - Is there a package that I can use in order to get rules for a target outcome in R 如何在另一个包中使用来自 R 包的“未导出”数据集 - How to use 'unexported' datasets from an R package within another package 如何将 MVN 包的输出放入 R 中的表中? - How can i get my output from MVN package into a table in R? 如何清除错误:对象 <some function> 不是&#39;名称空间导出: <some package> &#39;在构建我的R包时出现了什么? - How do I clear Error: object <some function> is not exported by 'namespace:<some package>' which shows up when building my R package? 如何使用R包中尚未导出的函数? - How can I use a function inside an R package that has not be exported? 如何在R中的“ivprobit”包中使用“ivprobit”功能? - How can I use the “ivprobit” function in “ivprobit” package in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM