Home » Lexical Scoping in R

Lexical Scoping in R

1980
  • Gerald Jay Sussman
  • Guy L. Steele Jr.
Computer programmer demonstrating lexical scoping in R programming language.

R uses lexical scoping, a concept inherited from the Scheme language. This means the values of free variables in a function are resolved by finding them in the environment where the function was defined, not the environment where it is called. This makes function behavior more predictable and independent of the calling context, a key feature for functional programming.

Lexical scoping, also known as static scoping, is a fundamental convention for looking up variable names in a programming language. In R, when a function needs to access a variable that is not defined locally (a “free variable”), it searches for that variable in the environment where the function was created. This “creation environment” is permanently attached to the function. This contrasts with dynamic scoping, used in some older languages, where the search would proceed up the chain of function calls (the “calling environment”).

For example, consider a function `f()` that uses a variable `x` but doesn’t define it. If `f()` is defined in the global environment where `x` is 10, then no matter where `f()` is called from, it will always use `x = 10`. Even if it’s called from another function that has its own local `x = 20`, `f()` will ignore the calling environment’s `x` and use the one from its definition environment. This behavior makes code easier to debug and reason about, as a function’s behavior is determined solely by its own code and the environment in which it was defined.

This feature is crucial for functional programming paradigms, enabling powerful constructs like closures, where a function can “remember” the environment in which it was created. This is heavily utilized in advanced R programming and package development, such as in the `lapply` family of functions and in frameworks like Shiny for web applications.

UNESCO Nomenclature: 1203
– Computer science

Type

Abstract System

Disruption

Incremental

Usage

Widespread Use

Precursors

  • The concept of scope in programming languages
  • The Lisp programming language and its dialects
  • The Scheme programming language, which popularized lexical scoping
  • Lambda calculus as a theoretical foundation for functions and scope

Applications

  • creation of closures in R for functional programming
  • development of package namespaces to avoid variable conflicts
  • enabling non-standard evaluation techniques used in packages like dplyr
  • building robust and modular code without side effects

Patents:

    Potential Innovations Ideas

    Professionals (100% free) Membership Required

    You must be a Professionals (100% free) member to access this content.

    Join Now

    Already a member? Log in here
    Related to: lexical scoping, static scoping, functional programming, R, Scheme, environment, closure, free variable, namespace, non-standard evaluation.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    AVAILABLE FOR NEW CHALLENGES
    Mechanical Engineer, Project, Process Engineering or R&D Manager
    Effective product development

    Available for a new challenge on short notice.
    Contact me on LinkedIn
    Plastic metal electronics integration, Design-to-cost, GMP, Ergonomics, Medium to high-volume devices & consumables, Lean Manufacturing, Regulated industries, CE & FDA, CAD, Solidworks, Lean Sigma Black Belt, medical ISO 13485

    We are looking for a new sponsor

     

    Your company or institution is into technique, science or research ?
    > send us a message <

    Receive all new articles
    Free, no spam, email not distributed nor resold

    or you can get your full membership -for free- to access all restricted content >here<

    Related Invention, Innovation & Technical Principles

    Scroll to Top

    You May Also Like