What is the working directory in R and how do options() and getOption() configure session behaviour?

Quick Overview

Explain what the R working directory controls and how options() and getOption() set and read session-wide settings. Tests understanding of relative path resolution, reproducible project paths, option defaults, and how to restore settings after changing them.

What is the working directory in R and how do options() and getOption() configure session behaviour?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

What is the working directory in R and how do `options()` and `getOption()` configure session behaviour? ```hint Think about relative paths Ask what a path like "data/sales.csv" is resolved against, and what changes when the same script runs on another machine. ``` ```hint Settings are global Consider how a function that changes an option should leave the session when it returns. ``` ### What a Strong Answer Covers - What the working directory is, how to read and change it, and how relative paths resolve against it - Why project-relative paths are more reproducible than `setwd()` with a hard-coded path - How `options()` sets and lists session settings and returns the previous values, and how `getOption()` reads one with a default - The scope of these settings: the current session, `.Rprofile` at startup, and restoring changes made inside functions ### Follow-up Questions - How would you change an option inside a function so it is restored even if the function fails? - Why can a heavy `.Rprofile` make your scripts fail on a colleague's machine? - How does the working directory behave when you knit an R Markdown document? - How would a package define its own option with a sensible default?

Overview: Explain what the R working directory controls and how options() and getOption() set and read session-wide settings. Tests understanding of relative path resolution, reproducible project paths, option defaults, and how to restore settings after changing them.

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

What is the working directory in R and how do options() and getOption() configure session behaviour?

What a Strong Answer Covers Guidance

  • What the working directory is, how to read and change it, and how relative paths resolve against it
  • Why project-relative paths are more reproducible than setwd() with a hard-coded path
  • How options() sets and lists session settings and returns the previous values, and how getOption() reads one with a default
  • The scope of these settings: the current session, .Rprofile at startup, and restoring changes made inside functions

Follow-up Questions Guidance

  • How would you change an option inside a function so it is restored even if the function fails?
  • Why can a heavy .Rprofile make your scripts fail on a colleague's machine?
  • How does the working directory behave when you knit an R Markdown document?
  • How would a package define its own option with a sensible default?