你如何使用“<<-"?(范围分配)在 R 中? [英] How do you use "<<-" (scoping assignment) in R?

查看:17
本文介绍了你如何使用“<<-"?(范围分配)在 R 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚读完了关于 R 介绍中的范围界定,并且对 <<- 赋值非常好奇.

I just finished reading about scoping in the R intro, and am very curious about the <<- assignment.

手册展示了一个(非常有趣的)<<- 示例,我觉得我理解了.我仍然缺少的是何时可以使用的上下文.

The manual showed one (very interesting) example for <<-, which I feel I understood. What I am still missing is the context of when this can be useful.

所以我很想从你那里读到一些关于何时使用 <<- 有趣/有用的例子(或例子的链接).使用它可能有什么危险(看起来很容易忘记),以及您可能想分享的任何提示.

So what I would love to read from you are examples (or links to examples) on when the use of <<- can be interesting/useful. What might be the dangers of using it (it looks easy to loose track of), and any tips you might feel like sharing.

推荐答案

<<- 与闭包结合使用以维护状态最有用.这是我最近一篇论文的一部分:

<<- is most useful in conjunction with closures to maintain state. Here's a section from a recent paper of mine:

闭包是由另一个函数编写的函数.关闭是之所以这么叫是因为它们封闭父级的环境函数,并且可以访问其中的所有变量和参数功能.这很有用,因为它允许我们有两个级别的参数.一级参数(父级)控制功能有效.另一个级别(孩子)负责工作.这下面的例子展示了如何使用这个想法来生成一个家庭功率函数.父函数 (power) 创建子函数(squarecube)实际上做了艰苦的工作.

A closure is a function written by another function. Closures are so-called because they enclose the environment of the parent function, and can access all variables and parameters in that function. This is useful because it allows us to have two levels of parameters. One level of parameters (the parent) controls how the function works. The other level (the child) does the work. The following example shows how can use this idea to generate a family of power functions. The parent function (power) creates child functions (square and cube) that actually do the hard work.

power <- function(exponent) {
  function(x) x ^ exponent
}

square <- power(2)
square(2) # -> [1] 4
square(4) # -> [1] 16

cube <- power(3)
cube(2) # -> [1] 8
cube(4) # -> [1] 64

在两个级别管理变量的能力还可以通过允许函数修改其父环境中的变量来维护跨函数调用的状态.在不同级别管理变量的关键是双箭头赋值运算符 <<-.不像通常的单箭头赋值 (<-) 总是在当前级别起作用,双箭头运算符可以修改父级别中的变量.

The ability to manage variables at two levels also makes it possible to maintain the state across function invocations by allowing a function to modify variables in the environment of its parent. The key to managing variables at different levels is the double arrow assignment operator <<-. Unlike the usual single arrow assignment (<-) that always works on the current level, the double arrow operator can modify variables in parent levels.

这使得维护一个记录函数被调用次数的计数器成为可能,如下例所示.每次运行new_counter,它都会创建一个环境,在这个环境中初始化计数器i,然后创建一个新函数.

This makes it possible to maintain a counter that records how many times a function has been called, as the following example shows. Each time new_counter is run, it creates an environment, initialises the counter i in this environment, and then creates a new function.

new_counter <- function() {
  i <- 0
  function() {
    # do something useful, then ...
    i <<- i + 1
    i
  }
}

新函数是一个闭包,它的环境是封闭环境.当闭包 counter_onecounter_two 运行时,每个闭包都会修改其封闭环境中的计数器,然后返回当前计数.

The new function is a closure, and its environment is the enclosing environment. When the closures counter_one and counter_two are run, each one modifies the counter in its enclosing environment and then returns the current count.

counter_one <- new_counter()
counter_two <- new_counter()

counter_one() # -> [1] 1
counter_one() # -> [1] 2
counter_two() # -> [1] 1

这篇关于你如何使用“&lt;&lt;-"?(范围分配)在 R 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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