R:承诺已经在评估中

sds*_*sds 6 callstack r lazy-evaluation

我知道您可能已经厌倦了再次回答相同的问题,但我仍然在其他几个 问题中讨论了错误:

承诺已经在评估中:递归默认参数引用还是早期问题?

即使我确实遵循了前期的"繁琐"建议".":

show.large.objects.threshold <- 100000
show.large.objects.exclude <- c("closure")
show.large.objects <- function (.envir = sys.frame(),
                                threshold = show.large.objects.threshold,
                                exclude = show.large.objects.exclude) {
  for (n in print(ls(.envir, all.names = TRUE))) tryCatch({
    o <- get(n,envir = .envir)
    s <- object.size(o)
    if (s > threshold && !(typeof(o) %in% exclude)) {
      cat(n,": ")
      print(s,units="auto")
    }
  }, error = function(e) { cat("n=",n,"\n"); print(e) })
}
show.large.objects.stack <- function (.threshold = show.large.objects.threshold,
                                      skip.levels = 1,# do not examine the last level - this function
                                      .exclude = show.large.objects.exclude) {
  for (level in 1:(sys.nframe()-skip.levels)) {
    cat("*** show.large.objects.stack(",level,") ")
    print(sys.call(level))
    show.large.objects(.envir = sys.frame(level), threshold = .threshold, exclude = .exclude)
  }
}
Run Code Online (Sandbox Code Playgroud)

但我仍然得到错误:

> f <- function () { c <- 1:1e7; d <- 1:1e6; print(system.time(show.large.objects.stack())) }
> f()
*** show.large.objects.stack( 1 ) f()
[1] "c" "d"
c : 38.1 Mb
d : 3.8 Mb
*** show.large.objects.stack( 2 ) print(system.time(show.large.objects.stack()))
[1] "..." "x"  
n= ... 
<simpleError in get(n, envir = .envir): argument "..." is missing, with no default>
n= x 
<simpleError in get(n, envir = .envir): promise already under evaluation: recursive default argument reference or earlier problems?>
*** show.large.objects.stack( 3 ) system.time(show.large.objects.stack())
[1] "expr"    "gcFirst" "ppt"     "time"   
n= expr 
<simpleError in get(n, envir = .envir): promise already under evaluation: recursive default argument reference or earlier problems?>
          user         system        elapsed 
    0 (0.00ms)     0 (0.00ms) 0.002 (2.00ms) 
Run Code Online (Sandbox Code Playgroud)
  1. 那么,我还在做什么呢?
  2. 难道我真的需要..envir?怎么样.exclude.threshold
  3. 为什么我会收到argument "..." is missing, with no default错误?
  4. 为什么我会收到promise already under evaluation错误?

谢谢!

Bac*_*lin 3

f被调用时,将构建 5 层的堆栈show.large.objects,它开始从顶部开始评估帧的内容。

f
  -> print
     -> system.time
        -> show.large.objects.stack
           -> show.large.objects
Run Code Online (Sandbox Code Playgroud)

1级

f()
Run Code Online (Sandbox Code Playgroud)

这里一切都好。

2级

print(system.time(show.large.objects.stack()))
Run Code Online (Sandbox Code Playgroud)

当你调用ls(.envir, all.names)它的框架时你会得到

[1] "..." "x"  
Run Code Online (Sandbox Code Playgroud)

其中...缺少并在调用时抛出错误 3 get,并且x = system.time(show.large.objects.stack())当前正在评估并抛出错误 4。

3级

system.time(show.large.objects.stack())
Run Code Online (Sandbox Code Playgroud)

ls给你

[1] "expr"    "gcFirst" "ppt"     "time"   
Run Code Online (Sandbox Code Playgroud)

其中expr = show.large.objects.stack()当前仍在评估中,并抛出另一个错误 4。

4级

show.large.objects.stack()
Run Code Online (Sandbox Code Playgroud)

ls内容不包含任何粗略的内容并且完整且没有错误。

底线

show.large.frames()必须单独进行评估,不能作为任何函数的参数,否则会抛出错误。为什么不让它自己打印呢?

我发现这非常有帮助

> debug(show.large.objects)
> f()
Browse[2]> lapply(sys.frames(), ls)
[[1]]
[1] "c" "d"

[[2]]
[1] "x"

[[3]]
[1] "expr"    "gcFirst" "ppt"     "time"   

[[4]]
[1] "level"       "skip.levels"

[[5]]
[1] "exclude"   "threshold"
Run Code Online (Sandbox Code Playgroud)