Explain the concept of lexical scoping in R and how environments affect variable access within functions

Quick Overview

Asks how lexical scoping works in R and how environments decide which value a function sees when it uses a name it did not define. Tests knowledge of enclosing environments, closures, the super-assignment operator and the difference from dynamic scoping.

Explain the concept of lexical scoping in R and how environments affect variable access within functions

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the concept of lexical scoping in R and how environments affect variable access within functions. ```hint Try a small case Write a function that uses a variable it does not define, then call it from inside another function that has its own variable with the same name. Which value does R pick, and what decides it? ``` ### What a Strong Answer Covers - What a free variable is and the rule R uses to resolve it - The order in which environments are searched, starting from the function's local environment - When a function's enclosing environment is fixed, and how that makes closures possible - How lexical scoping differs from dynamic scoping, with a case where the two give different results - What `<<-` does and at least one common scoping mistake ### Follow-up Questions - What happens when `<<-` assigns to a name that does not exist in any enclosing environment? - How do `environment()`, `environment(f)` and `parent.frame()` differ, and when would a function need the caller's environment? - Closures created in a `for` loop all return the same result when called. Why, and how do you fix it? - In what order does R search for a free variable used by a function that lives in a package namespace?

Overview: Asks how lexical scoping works in R and how environments decide which value a function sees when it uses a name it did not define. Tests knowledge of enclosing environments, closures, the super-assignment operator and the difference from dynamic scoping.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the concept of lexical scoping in R and how environments affect variable access within functions.

What a Strong Answer Covers Guidance

  • What a free variable is and the rule R uses to resolve it
  • The order in which environments are searched, starting from the function's local environment
  • When a function's enclosing environment is fixed, and how that makes closures possible
  • How lexical scoping differs from dynamic scoping, with a case where the two give different results
  • What <<- does and at least one common scoping mistake

Follow-up Questions Guidance

  • What happens when <<- assigns to a name that does not exist in any enclosing environment?
  • How do environment() , environment(f) and parent.frame() differ, and when would a function need the caller's environment?
  • Closures created in a for loop all return the same result when called. Why, and how do you fix it?
  • In what order does R search for a free variable used by a function that lives in a package namespace?