在环境中分配列表属性
标题是一个独立的问题。一个例子可以澄清这一点:考虑一下
x=list(a=1, b="name")
f <- function(){
assign('y[["d"]]', FALSE, parent.frame() )
}
g <- function(y) {f(); print(y)}
g(x)
$a
[1] 1
$b
[1] "name"
,而我想得到
g(x)
$a
[1] 1
$b
[1] "name"
$d
[1] FALSE
一些评论。我知道我原来的例子有什么问题,但我用它来明确我的目标。我想避免 <<-,并希望 x 在父框架中更改。
我认为我对环境的理解很原始,任何参考资料都会受到赞赏。
The title is the self-contained question. An example clarifies it: Consider
x=list(a=1, b="name")
f <- function(){
assign('y[["d"]]', FALSE, parent.frame() )
}
g <- function(y) {f(); print(y)}
g(x)
$a
[1] 1
$b
[1] "name"
whereas I would like to get
g(x)
$a
[1] 1
$b
[1] "name"
$d
[1] FALSE
A few remarks. I knew what is wrong in my original example, but am using it to make clear my objective. I want to avoid <<-, and want x to be changed in the parent frame.
I think my understanding of environments is primitive, and any references are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
assign
的第一个参数必须是变量名,而不是表达式的字符表示形式。尝试将f
替换为:请注意,
a
、b
和d
是列表组件,而不是列表属性。如果我们想在f
的父框架中向y
添加属性"d"
,我们会这样做:另外,请注意,取决于你想要做什么,让
x
成为一个环境或一个 proto 对象(来自 proto 包)可能会(也可能不会)更好。The first argument to
assign
must be a variable name, not the character representation of an expression. Try replacingf
with:Note that
a
,b
andd
are list components, not list attributes. If we wanted to add an attribute"d"
toy
inf
's parent frame we would do this:Also, note that depending on what you want to do it may (or may not) be better to have
x
be an environment or a proto object (from the proto package).assign
的第一个参数必须是对象名称。您对assign
的使用与分配帮助页面末尾的反例基本相同。观察:这可能是您想要使用“<<-”的地方,但它通常被认为是可疑的。
在本练习没有任何目标并忽略 Gabor 指出的术语“属性”的情况下,提出了进一步的想法,该术语在 R 中具有特定含义,但可能不是您的目标。如果您想要的只是输出符合您的规格,那么这可以实现该目标,但请注意全局环境中的 x 没有发生变化。
f
的parent.frame 可能被称为“g
的内部”,但更改不会传播到全局环境。assign
's first argument needs to be an object name. Your use ofassign
is basically the same as the counter-example at the end of the the assign help page. Observe:This may be where you want to use "<<-" but it's generally considered suspect.
A further thought, offered in the absence of any goal for this exercise and ignoring the term "attributes" which Gabor has pointed out has a specific meaning in R, but may not have been your goal. If all you want is the output to match your specs then this achieves that goal but take notice that no alteration of x in the global environment is occurring.
The parent.frame for
f
is what might be called the "interior ofg
but the alteration does not propagate out to the global environment.