Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得在R中计算表达式之前需要定义的所有变量?_R - Fatal编程技术网

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

如何获得在R中计算表达式之前需要定义的所有变量?,r,R,如果我有一个表达: z <- x+y z需要剥离,以便向用户建议在计算表达式之前需要定义哪些值。我想您是在问如何可以忽略表达式的左侧 txt <- "z <- x+y" p <- parse(text=txt) 只要您不评估解析文本,我认为您应该不会被用户输入这样的内容: txt <- "z <- system('rm -Rf *')" txt这里有一个可能的解决方案,假设多参数函数从左到右计算其参数。这对于典型的二进制运算符是正确的,这似乎是您感兴趣的

如果我有一个表达:

z <- x+y
z
需要剥离,以便向用户建议在计算表达式之前需要定义哪些值。

我想您是在问如何可以忽略表达式的左侧

txt <- "z <- x+y"
p <- parse(text=txt)
只要您不评估解析文本,我认为您应该不会被用户输入这样的内容:

txt <- "z <- system('rm -Rf *')"

txt这里有一个可能的解决方案,假设多参数函数从左到右计算其参数。这对于典型的二进制运算符是正确的,这似乎是您感兴趣的,但正如我在对@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)
}

您想要的解决方案需要多大的通用性?R中赋值表达式的左侧可能包含许多复杂的内容。这个问题与之有什么不同?@RichardScriven可能不同之处在于他们希望找到那些尚未定义的内容,例如
setdiff(all.vars(quote(x+y)),ls()
?@nongkrong…或者
res@joran…你的意思是:如果左手边包含一个需要计算的索引表达式,那么这会处理这种情况吗?e、 g.a
[我将使用opencpu,所以我认为像system这样的危险功能被禁用。@BenBolker我不明白为什么不期望
parse
返回表达式对象?这就是它的帮助页面所说的。@joran:
txt@bondedust这是意料之中的;我不得不使用
[[1][对我来说]是令人沮丧的]
剥离到我真正想要的东西。
all.vars(p2[[3]])
## [1] "x" "y"    
txt <- "z <- system('rm -Rf *')"
txt <- "names(x)[1] <- as.character(a * log(x))"
all.vars(parse(text=txt)[[1]][[3]])
## [1] "a" "x"
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(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"