What do do.call, match.arg, and on.exit do in R?

Quick Overview

Asks what the R functions do.call, match.arg and on.exit do and when you would reach for each while writing a function. Tests how well you know R's tools for building calls from lists, checking enumerated string arguments, and cleanup that must run when a function exits.

What do do.call, match.arg, and on.exit do in R?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

What do `do.call()`, `match.arg()` and `on.exit()` do in R, and when would you use each one while writing a function? ```hint Start from the problem each one solves For each function, name the situation inside a function you are writing where a plain call falls short, then show a one-line use. ``` ### What a Strong Answer Covers - What `do.call()` does with its function and list arguments, and a case where a direct call does not work - Where `match.arg()` gets its allowed values, what it returns when the caller gives nothing, and how it fails - When code registered with `on.exit()` runs, and how several handlers in one function behave - A short example function that uses them ### Follow-up Questions - Why does `rbind(dfs)` not combine a list of data frames when `do.call(rbind, dfs)` does? - What happens if you call `on.exit()` twice in the same function without `add = TRUE`? - How would you let a caller choose more than one option with `match.arg()`? - How do `withr::defer()` and `withr::local_options()` relate to `on.exit()`?

Overview: Asks what the R functions do.call, match.arg and on.exit do and when you would reach for each while writing a function. Tests how well you know R's tools for building calls from lists, checking enumerated string arguments, and cleanup that must run when a function exits.

Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

What do do.call(), match.arg() and on.exit() do in R, and when would you use each one while writing a function?

What a Strong Answer Covers Guidance

  • What do.call() does with its function and list arguments, and a case where a direct call does not work
  • Where match.arg() gets its allowed values, what it returns when the caller gives nothing, and how it fails
  • When code registered with on.exit() runs, and how several handlers in one function behave
  • A short example function that uses them

Follow-up Questions Guidance

  • Why does rbind(dfs) not combine a list of data frames when do.call(rbind, dfs) does?
  • What happens if you call on.exit() twice in the same function without add = TRUE ?
  • How would you let a caller choose more than one option with match.arg() ?
  • How do withr::defer() and withr::local_options() relate to on.exit() ?