您如何使用“<<-" (作用域确定)在R中? [英] How do you use "<<-" (scoping assignment) in R?

查看:93
本文介绍了您如何使用“<<-" (作用域确定)在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. 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天全站免登陆