What is the difference between attaching and loading a package in R?

Quick Overview

Asks how loading a package in R differs from attaching it. Tests understanding of namespaces, the search path and name masking, how package dependencies are brought into a session, and which calls to use in scripts compared with package code.

What is the difference between attaching and loading a package in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the difference between attaching and loading a package in R? ```hint Watch two lists Compare what `search()` and `loadedNamespaces()` return in a fresh session, after calling a function as `pkg::fun()`, and again after `library(pkg)`. ``` ### What a Strong Answer Covers - What loading does, and which calls cause it - What attaching adds, and which calls cause it - How the search path decides which function a bare name refers to, and what masking means - How a package's own dependencies are loaded or attached - Practical guidance for analysis scripts compared with package code ### Follow-up Questions - `library(dplyr)` reports that `filter` and `lag` are masked from `stats`. What does that mean, and how do you call the masked versions? - How do `library()`, `require()` and `requireNamespace()` differ, and when would you use each? - Why should code inside a package never call `library()`? - How do you remove a package from a session, and why might its namespace stay loaded?

Overview: Asks how loading a package in R differs from attaching it. Tests understanding of namespaces, the search path and name masking, how package dependencies are brought into a session, and which calls to use in scripts compared with package code.

PracHub logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

What is the difference between attaching and loading a package in R?

What a Strong Answer Covers Guidance

  • What loading does, and which calls cause it
  • What attaching adds, and which calls cause it
  • How the search path decides which function a bare name refers to, and what masking means
  • How a package's own dependencies are loaded or attached
  • Practical guidance for analysis scripts compared with package code

Follow-up Questions Guidance

  • library(dplyr) reports that filter and lag are masked from stats . What does that mean, and how do you call the masked versions?
  • How do library() , require() and requireNamespace() differ, and when would you use each?
  • Why should code inside a package never call library() ?
  • How do you remove a package from a session, and why might its namespace stay loaded?