<-和<<-之间的差异 [英] Difference between <- and <<-

查看:101
本文介绍了<-和<<-之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况1:

rm(list = ls())
foo <- function(x = 6){
  set <- function(){
  x <- x*x}
  set()
x}
foo()
# [1] 6

情况2:

rm(list = ls())
foo <- function(x = 6){
set <- function(){
  x <<- x*x}
  set()
  x}
foo()
# [1] 36

我读到<<-运算符可用于在不同于当前环境的环境中为对象分配值.它说可以使用<<-对当前环境中不存在的对象进行对象初始化.我想问一下可以使用<<-初始化哪个环境的对象.在我的情况下,环境是foo函数的环境,<<-可以初始化函数外部的对象还是当前环境中的对象?何时使用<-和何时使用<<-完全令人困惑.

I read that <<- operator can be used to assign a value to an object in an environment that is different from the current environment. It says that object initialization using <<- can be done to the objects that is not in the current environment. I want to ask which environment's object can be initialized using <<- . In my case the environment is environment of foo function, can <<-initialize the objects outside the function or the object in the current environment? Totally confused when to use <- and when to use <<-.

推荐答案

运算符<<-是父作用域分配运算符.它用于对 nearest 父范围中的变量进行赋值,以对其进行评估.因此,这些分配粘在"函数调用之外的范围内.考虑以下代码:

The operator <<- is the parent scope assignment operator. It is used to make assignments to variables in the nearest parent scope to the scope in which it is evaluated. These assignments therefore "stick" in the scope outside of function calls. Consider the following code:

fun1 <- function() {
    x <- 10
    print(x)
}

> x <- 5    # x is defined in the outer (global) scope
> fun1()
[1] 10      # x was assigned to 10 in fun1()
> x
[1] 5       # but the global value of x is unchanged

在函数fun1()中,将 local 变量x分配给值10,但是在全局范围内,x的值未更改.现在考虑重写该函数以使用父作用域赋值运算符:

In the function fun1(), a local variable x is assigned to the value 10, but in the global scope the value of x is not changed. Now consider rewriting the function to use the parent scope assignment operator:

fun2 <- function() {
    x <<- 10
    print(x)
}

> x <- 5
> fun2()
[1] 10      # x was assigned to 10 in fun2()
> x
[1] 10      # the global value of x changed to 10

由于函数fun2()使用<<-运算符,因此在函数完成评估后,x的分配将粘滞". R的实际作用是遍历fun2()以外的所有范围,并查找包含名为x的变量的 first 范围.在这种情况下,fun2()之外的唯一作用域是全局作用域,因此它将在其中进行分配.

Because the function fun2() uses the <<- operator, the assignment of x "sticks" after the function has finished evaluating. What R actually does is to go through all scopes outside fun2() and look for the first scope containing a variable called x. In this case, the only scope outside of fun2() is the global scope, so it makes the assignment there.

正如一些人已经评论过的那样,<<-运算符被很多人所反对,因为它可能破坏R脚本的封装.如果我们将R函数视为一个孤立的功能,则不应允许它干扰调用它的代码的状态.滥用<<-赋值运算符会带来这样做的风险.

As a few have already commented, the <<- operator is frowned upon by many because it can break the encapsulation of your R scripts. If we view an R function as an isolated piece of functionality, then it should not be allowed to interfere with the state of the code which calls it. Abusing the <<- assignment operator runs the risk of doing just this.

这篇关于&lt;-和&lt;&lt;-之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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