Skip to main content

Scope and Closures

  • Chapter
  • First Online:
Functional Programming in R 4
  • 400 Accesses

Abstract

A scope or environment is something functions and expressions are associated with that tells them what value each variable refers to. It is used to figure out which environment expressions are evaluated in. The same variable name can be used in many places in a program, referring to different things in different locations, but the scope of an expression tells R exactly how to map names to values. In R, you have both implicit environments—that I will tend to call scopes—and explicit environments. The former is what you will use in most everyday programming, while the latter is used in more advanced techniques, which mostly add flexibility in how you can provide interfaces to users. The way that R maps names to values is the same in both settings, however, and understanding how this works can make it easier to write functions and understand advanced packages.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    Packages also use the environment system to differentiate between public and private names, but not through a separate mechanism. They just use different environment chains, where we hook into the public environment while they have another environment containing the private names that we do not see.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2023 The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Mailund, T. (2023). Scope and Closures. In: Functional Programming in R 4. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-9487-1_4

Download citation

Publish with us

Policies and ethics