r - 如何获取在计算 R 表达式之前需要定义的所有变量?

标签 r

如果我有一个表达式:

z <- x+y

xy 需要定义,而 z 则不需要。如何只获取 xy

missing_values('z<-x+y');
[1] "x" "y"

z 需要被删除,以便向用户建议在计算表达式之前需要定义哪些值。

最佳答案

这是一个可能的解决方案,假设多参数函数从左到右评估其参数。这对于典型的二元运算符来说是正确的,这正是您似乎感兴趣的,但正如我在对 @BenBolker 的答案的评论中指出的那样,这并不普遍正确。

find_unbound <- function(lang) {
  stopifnot(is.language(lang), !is.expression(lang))

  bound <- character()
  unbound <- character()

  rec_fun <- function(lang) {
    if(is.call(lang)) {
      # These are assignment calls; if any symbols are assigned and have
      # not already been found in a leaf, they are defined as bound

      if((lang[[1]] == as.name("<-") || lang[[1]] == as.name("="))) {
        for(i in as.list(lang)[-(1:2)]) Recall(i)
        if(is.name(lang[[2]]) && !as.character(lang[[2]]) %in% unbound)
          bound <<- c(bound, as.character(lang[[2]]))
      } else for(i in as.list(lang)[-1]) Recall(i)                
    } else if (is.name(lang) && ! as.character(lang) %in% bound)
      # this is a leaf; any previously bound symbols are by definition
      # unbound

      unbound <<- c(unbound, as.character(lang))
  }
  rec_fun(lang)
  unique(unbound)
}

find_unbound 一直递归到表达式的叶子节点,以确定每个符号是否已被绑定(bind)。下面是一些测试来说明:

find_unbound(quote(z <- x + y))
# [1] "x" "y"
find_unbound(quote(z <- x + (y <- 3)))
# [1] "x"
find_unbound(quote(z <- z + 1))           # we even figure out `z` depends on itself, so must be provided
# [1] "z"
find_unbound(quote(z <- x + (x <- 3)))    # note `x` is evaluated before `x <- 3`
# [1] "x"
find_unbound(quote(z <- (x <- 3) + x))    # but here `x <- 3` is evaluated before `x`
# character(0)
find_unbound(quote({x <- 3; z <- x + y})) # works with multiple calls
# [1] "y"
find_unbound(quote({z <- x + y; x <- 3})) # order matters, just like in R evaluation
# [1] "x" "y"

关于r - 如何获取在计算 R 表达式之前需要定义的所有变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32363338/

相关文章:

r - 使用 href 信息框作为操作按钮

r - 为 `rbind` 调度 `cbind` 和 `data.frame`

r - 使用 .SDcols 时将 data.table 列传递给函数

r - 分发使用外部软件包的R脚本

r - 从数据框中聚合多列

postgresql - 在 PL/R 中加载、列出和使用 R 模块和函数

r - 使用 `file::function` 命名间距 R 文件的最简单方法是什么

r - 使用Rvest登录网站抓取403错误

r - 非标准评估、lapply 和 ggplot

r - 安装 rgdal 时遇到问题