神秘的形式(函数(x){})$ x [英] The arcane formals(function(x){})$x

查看:129
本文介绍了神秘的形式(函数(x){})$ x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是对象formals(function(x){})$x?

可以在函数的形式中找到,绑定到没有默认值的参数上.

It's found in the formals of a function, bound to arguments without default value.

还有其他方法可以引用这个奇怪的对象吗?除了代表空的函数参数之外,它还有其他作用吗?

Is there any other way to refer to this strange object? Does it have some role other than representing an empty function argument?

以下是可以在控制台中检查的一些属性:

Here are some of its properties that can be checked in the console:

> is(formals(function(x){})$x)
[1] "name"      "language"  "refObject"
> formals(function(x){})$x

> as.character(formals(function(x){})$x)
[1] ""

这是获取此对象的其他一些方法:

Here are some other ways to get this object:

alist(,)[[1]]
bquote()
quote(expr=)

推荐答案

背景:什么是formals(function(x) {})?

好吧,formals(function(x) {})首先(并记录在?formals中)返回配对列表:

Background: What is formals(function(x) {})?

Well, to start with (and as documented in ?formals) , formals(function(x) {}) returns a pairlist:

is(formals(function(x){}))
# [1] "pairlist"

list 对象不同, pairlist 对象可以具有不包含值的命名元素-在构造具有可能为可选的形式参数的函数时,这是一件非常好的事情.来自?pairlist:

Unlike list objects, pairlist objects can have named elements that contain no value -- a very nice thing when constructing a function that has a possibly optional formal argument. From ?pairlist:

允许使用

没有值的带标签的参数,而列表"只是忽略它们.

tagged arguments with no value are allowed whereas ‘list’ simply ignores them.

要了解差异,将创建对表的alist()与构建普通表"列表的list()进行比较:

To see the difference, compare alist(), which creates pairlists, with list() which constructs 'plain old' lists:

list(x=, y=2)
# Error in list(x = , y = 2) : argument 1 is empty

alist(x=, y=2)
# $x
# 
# $y
# [1] 2

您的问题:什么是formals(function(x) {})$x?

现在是关于formals(function(x) {})$x是什么的问题.从某种意义上说,我的理解是它的真实值是空符号".但是,您无法从R内部获得它,因为空符号"是R的开发人员(非常有意设计)试图完全向R用户隐藏的对象. (要对空符号及其为何保持隐藏状态进行有趣的讨论,请

Your question: What is formals(function(x) {})$x?

Now to your question about what formals(function(x) {})$x is. My understanding is in some sense its real value is the "empty symbol". You can't, however, get at it from within R because the "empty symbol" is an object that R's developers -- very much by design -- try to entirely hide from R users. (For an interesting discussion of the empty symbol, and why it's kept hidden, see the thread starting here).

当尝试通过索引成对列表的空值元素来达到目的时,R的开发人员通过让R返回该元素的 name 而不是其公共文本来挫败这一尝试. -查看. (当然,这是问题中显示的 name 对象).

When one tries to get at it by indexing an empty-valued element of a pairlist, R's developers foil the attempt by having R return the name of the element instead of its verbotten-for-public-viewing value. (This is, of course, the name object shown in your question).

这篇关于神秘的形式(函数(x){})$ x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆