Product Design, Manufacturing & Innovation Resources
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.

(generated image for illustration only)

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

    Due to scrapping bot traffic, currently more than 40k per day, this content is reserved to community members.
    > Login < or > Register < (100% free) to access this, so as all other restricted content and tools.

    Related to: lexical scoping, static scoping, functional programming, R, Scheme, environment, closure, free variable, namespace, non-standard evaluation.

    Historical Context

    Lexical Scoping in R

    1973
    1980
    1980
    1980
    1982-07-01
    1988-06-01
    1990
    1970-01-01
    1975-06-01
    1980
    1980
    1980
    1986-01-01
    1990
    1990

    (if date is unknown or not relevant, e.g. "fluid mechanics", a rounded estimation of its notable emergence is provided)

    Related Invention, Innovation & Technical Principles

    Full size images and downloads are only available, 100% free, for registered members.

    > Login <