Explain the scope of variables in R, including local and global variables.

Quick Overview

Asks how variable scope works in R, covering local and global variables, environments and lexical scoping. Tests whether you know how R looks up a name, what the two assignment operators do inside a function, and how closures keep local variables alive after a call returns.

Explain the scope of variables in R, including local and global variables.

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain the scope of variables in R, including local and global variables. Cover where a variable created inside a function can be seen, how a function finds a variable it did not create, and what happens when a function assigns to a name that also exists outside it. ```hint Think in environments Each function call runs in an environment that has a parent. Work out which environment that parent is, and whether assignment follows the same path as lookup. ``` ### What a Strong Answer Covers - Environments as the mechanism behind scope, and which environment becomes the parent of a function call. - How R looks up a name, moving outward to the global environment and the search path, and what shadowing means. - What `<-` inside a function does to a name that also exists globally, and how `<<-` and `assign()` behave differently. - What happens to local variables when the call returns, including closures that keep them alive. - Practical guidance on passing arguments and returning values instead of relying on globals. ### Follow-up Questions - What does `<<-` do when no enclosing environment has the variable? - Write a counter generator using a closure. Why does each counter keep its own count? - How does lexical scoping differ from dynamic scoping, and why does a function not see its caller's local variables? - A function works in your session but fails on a colleague's machine. How could a global variable cause that, and how would you find it?

Overview: Asks how variable scope works in R, covering local and global variables, environments and lexical scoping. Tests whether you know how R looks up a name, what the two assignment operators do inside a function, and how closures keep local variables alive after a call returns.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the scope of variables in R, including local and global variables.

Cover where a variable created inside a function can be seen, how a function finds a variable it did not create, and what happens when a function assigns to a name that also exists outside it.

What a Strong Answer Covers Guidance

  • Environments as the mechanism behind scope, and which environment becomes the parent of a function call.
  • How R looks up a name, moving outward to the global environment and the search path, and what shadowing means.
  • What <- inside a function does to a name that also exists globally, and how <<- and assign() behave differently.
  • What happens to local variables when the call returns, including closures that keep them alive.
  • Practical guidance on passing arguments and returning values instead of relying on globals.

Follow-up Questions Guidance

  • What does <<- do when no enclosing environment has the variable?
  • Write a counter generator using a closure. Why does each counter keep its own count?
  • How does lexical scoping differ from dynamic scoping, and why does a function not see its caller's local variables?
  • A function works in your session but fails on a colleague's machine. How could a global variable cause that, and how would you find it?