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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 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
Rights and permissions
Copyright information
© 2023 The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature
About this chapter
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
DOI: https://doi.org/10.1007/978-1-4842-9487-1_4
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-9486-4
Online ISBN: 978-1-4842-9487-1
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books