- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以在 Hadley 的高级 R 书中,有一个使用 substitute 的例子,这里是代码的摘录:
subset2 <- function(x, condition) {
condition_call <- substitute(condition)
r <- eval(condition_call, x, parent.frame())
x[r, ]
}
scramble <- function(x) x[sample(nrow(x)), ]
subscramble <- function(x, condition) {
scramble(subset2(x, condition))
}
subscramble(sample_df, a >= 4)
# Error in eval(expr, envir, enclos) : object 'a' not found
traceback()
#> 5: eval(expr, envir, enclos)
#> 4: eval(condition_call, x, parent.frame()) at #3
#> 3: subset2(x, condition) at #1
#> 2: scramble(subset2(x, condition)) at #2
#> 1: subscramble(sample_df, a >= 4)
Can you see what the problem is? condition_call contains the expression condition. So when we evaluate condition_call it also evaluates condition, which has the value a >= 4. However, this can’t be computed because there’s no object called a in the parent environment. But, if a were set in the global environment, even more confusing things can happen:
书中的上述段落有几处让我感到困惑。
句子“condition_call contains the expression condition”。符号“condition”在函数 subset2 中用作形式参数,也在 scramble(subset2(x,condition)) 中用作实参数。我猜他指的是这个真实的/调用的参数“条件”,对吗?
作为 promise ,subscramble 定义中的条件是惰性求值的?为什么调用时不求值:争夺(子集2(x,条件))
换句话说,我如何通过查看代码知道一个 promise 是否被求值?比如我理解正确的话,如果我把代码改成下面这样:
scramble(subset2(x,(condition)))
现在强制评估条件。这里的规则是什么?
Hadley 说“当我们评估 condition_call 时,它也会评估条件”,“它”是什么?他的意思是“评估”触发了某种试图解决 promise “条件”的内部或二次评估吗?这发生在哪里?即 R 试图使用什么环境来找出“条件”的值是什么?
所以错误“找不到对象”不是由于下面调用中的“x”或“parent.frame()”,而是其他地方?我完全糊涂了。
r <- eval(condition_call, x, parent.frame())
最佳答案
我无法发表评论,所以我会将其作为答案发布。一切都将只是对 :
Non standard evaluation from another function in R
本质上,在 sample_df 调用环境中,函数将寻找“条件”而不是“a>=4”。由于找不到它,它会向上移动,然后在调用环境中找到条件; subscramble 执行环境(这是因为 subset2(x, condition) 是在此环境中创建的 promise )它发现 a>=4。
现在它需要找到 a,但我们已经离开了 sample_df 数据环境,因此它在全局环境中搜索它,如果在全局环境中定义了 a,则会导致奇怪的结果。
关于r - 非标准评估,高级R书中的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31479813/
我的英语很差,抱歉 这是我的结构: bookstore ---author(app1) ---book(app2) 或者在代码中: from django.db import models from
我对antlr 的理解停留在非常基础的层面。浏览 Parr 博士的“权威 ANTLR 4 引用”。在第 4.2 节“使用访问者构建计算器”中列出了以下语法: grammar LabeledExpr;
我对antlr 的理解停留在非常基础的层面。浏览 Parr 博士的“权威 ANTLR 4 引用”。在第 4.2 节“使用访问者构建计算器”中列出了以下语法: grammar LabeledExpr;
我正在学习 1.1.0 的 rust book 教程,但尝试运行他们的代码时出现错误。 我有以下内容: extern crate rand; use std::io; use std::cmp::Or
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我的帖子是书的代码。 void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *)); /*Why it
考虑到 Fluent NHibernate 已经有一段时间了,我想应该会有一本书可以买到,所以我在亚马逊和谷歌中搜索,但没有关于 fluent nhiberanet 的书籍。我是对的还是这本书已经存在
考虑到 DocBook 格式的书可以以“模块化”的方式完成,我希望我可以用 AsciiDoc 做类似的事情,并将章节和第一级部分拆分到单独的文件中。不幸的是,文档没有提及这一点。到目前为止,我看到的唯
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我目前正在使用 Jquery 使用 turn.js 制作一本书,进展顺利,但我需要一点帮助。你看,我正在尝试制作一本精装期刊类型的书,就像此处显示的示例一样 http://www.turnjs.com
我正在阅读 Bjarne S 的 C++ 编程语言。 在第 77 页,第 4.8 节中,我发现了这一点: "枚举器可以用常量表达式初始化(§C.5) 的整数类型 (§4.1.1)。这枚举的范围包含向上
为什么当我输入由空格分隔的字符串(在 while 循环之外)并且我尝试在屏幕上打印它们时,只有我输入的第一个出现,而在这个 while 循环中(见代码)它打印所有这些一个? //this one pr
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在读这本书并被困在这里: public static class EventArgExtensions { public static void Raise(this TEventArgs
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在尝试使用 xlwt 创建具有多个选项卡的输出文件(.xlsx 格式)。我的Python版本号是2.7,IDE是Aptana Studio 3。 我以前使用过 xlwt 包,在相同的环境下执行相同
如何使用 Xcode 或 macOS 打开 Swift Playground Book?当我打开它时,我得到了这个: 最佳答案 在 Xcode 上,转到 Navigate > Reveal in Pr
嗨,我对 php 很陌生,我正在阅读 Murach PHP 书。我在单击类别部分中的链接时遇到问题,因为当它单击该链接时,它会广告 ?category_id=3 但问题是它在 add_product.
我是一名优秀的程序员,十分优秀!