Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 720681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:47:12+00:00 2026-05-14T05:47:12+00:00

I just finished reading about scoping in the R intro , and am very

  • 0

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.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:47:12+00:00Added an answer on May 14, 2026 at 5:47 am

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

    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.

    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
      }
    }
    

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just finished reading Jon Skeet's article about events and delegates and got a question.
I'm very new to Perl programming. I've just finished reading the Llama book. Up
Just finished reading the latest stackoverflow blog post about moving static content to a
I've just finished reading about SOAP via Spring-WS in Spring in Action, 2nd edition,
I am just about finished reading K&R, and that is all the C that
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I just finished reading the HTML5 Developer's Cookbook and have a question. I've read
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
I've just finished reading Roy Osherove's The Art of Unit Testing and I am
I just finished reading the Functions chapter from Uncle Bob's Clean Code . The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.