`do.call` blows up final object size (returned object is not identical to direct function call)
10:08 27 Jul 2026

do.call "constructs and executes a function call from a name or a function and a list of arguments to be passed to it".

That is, in theory these two lines hsould return the same object:

> out1 = lm(mpg~wt, data = mtcars)
> out2 = do.call(lm, list(mpg~wt, data = mtcars))

But that is not the case, as do.callseems to copy the entire formula body of lm and the entire dataframe into the final object.

See the code below.

Why does that happen, and what other function can I use to call a function with a list of parameters and produce the exact same object that the direct function call would produce?

Notice that rlang::exec(lm, !!!list(mpg ~ wt, data = mtcars)) doesn't work either (see code below)

## -----------------------------------------------------------------------
## do.call() substitutes argument VALUES into the call it builds
##
## Models store the call that created them. Fitting through do.call() therefore
## embeds whatever you passed -- including the entire data frame, and even the
## body of the fitting function itself -- inside every fitted object.
##
## Reproducible on a clean R session; only stats/utils are used.
## -----------------------------------------------------------------------

sz  <- function(x) format(utils::object.size(x), units = "KB")
cl  <- function(m, n = 60) {
  s <- paste(deparse(m$call), collapse = " ")
  if (nchar(s) > n) paste0(substr(s, 1, n), " ...") else s
}

## ---- 1. the four ways to make the same fit -----------------------------

a <- lm(mpg ~ wt, data = mtcars)                                  # ordinary
b <- do.call(lm,   list(mpg ~ wt, data = mtcars))                 # function object
c <- do.call("lm", list(mpg ~ wt, data = mtcars))                 # function NAME
d <- do.call("lm", list(quote(mpg ~ wt), data = quote(mtcars)))   # name + symbols
e <- rlang::exec(lm, !!!list(mpg ~ wt, data = mtcars))                 # function object

stopifnot(all.equal(coef(a), coef(b)), all.equal(coef(a), coef(c)),
          all.equal(coef(a), coef(d)), all.equal(coef(a), coef(e)))          # identical fits throughout

res <- data.frame(
  how = c("lm(mpg ~ wt, data = mtcars)",
          'do.call(lm,   list(f, data = df))',
          'do.call("lm", list(f, data = df))',
          'do.call("lm", list(quote(f), quote(df)))',
        'rlang::exec(lm, !!!list(mpg ~ wt, data = mtcars))'),
  size = c(sz(a), sz(b), sz(c), sz(d), sz(e)),
  call = c(cl(a), cl(b), cl(c), cl(d), cl(e)),
  stringsAsFactors = FALSE
)
print(res, right = FALSE, row.names = FALSE)

## Typical output (R 4.6.1, mtcars is only 7 KB):
##
##  how                                      size     call
##  lm(mpg ~ wt, data = mtcars)              24.9 Kb  lm(formula = mpg ~ wt, data = mtcars)
##  do.call(lm,   list(f, data = df))        231.4 Kb (function (formula, data, subset, weights, ...
##  do.call("lm", list(f, data = df))        32.3 Kb  lm(formula = mpg ~ wt, data = structure(lis ...
##  do.call("lm", list(quote(f), quote(df))) 24.9 Kb  lm(formula = mpg ~ wt, data = mtcars)
##
## Two distinct leaks, and they add up:
##   * passing the FUNCTION OBJECT inlines the whole body of lm() into the call
##   * passing the DATA inlines the entire data frame into the call
## Passing the function by name fixes the first; passing symbols fixes the second.

## ---- 2. it scales with the data, not with the model --------------------

big <- data.frame(y = rnorm(20000), x = rnorm(20000))
for (k in 1:20) big[[paste0("unused", k)]] <- rnorm(20000)   # columns no model uses

m_val <- do.call("lm", list(y ~ x, data = big))
m_sym <- do.call("lm", list(quote(y ~ x), data = quote(big)))

cat("\ndata frame:            ", sz(big), "\n")
cat("fit, data inlined:     ", sz(m_val), "\n")
cat("fit, data by symbol:   ", sz(m_sym), "\n")
cat("of which the $call is: ", sz(m_val$call), "vs", sz(m_sym$call), "\n")

## The columns the model never uses are copied too: do.call() embeds the object
## you passed, not the model frame.

## ---- 3. why it bites in practice ---------------------------------------
##
## Any wrapper that assembles arguments programmatically reaches for do.call():
##
##   fit_one <- function(spec) do.call(fitter, modifyList(shared, spec))
##
## with `shared` holding `data = `. Fit 8 models that way and
## each carries its own copy of the data inside $call. The fix is to build the
## call so it refers to things by name:

fit_by_name <- function(fitter_name, args, data_name = "data", env = parent.frame()) {
  e <- new.env(parent = env)
  if (!is.null(args$data)) {
    assign(data_name, args$data, envir = e)
    args$data <- as.name(data_name)              # a symbol, not the frame
  }
  do.call(fitter_name, args, envir = e)          # the NAME, not the function
}

m_fix <- fit_by_name("lm", list(y ~ x, data = big), data_name = "big")
cat("\nvia the wrapper:       ", sz(m_fix), "|", cl(m_fix), "\n")
stopifnot(all.equal(coef(m_val), coef(m_fix)))

## ---- 4. caveats --------------------------------------------------------
##
## * What the call records must still be resolvable when something later
##   re-evaluates it (update(), predict() with new data, insight::get_data()).
##   A symbol that no longer exists gives "object 'big' not found".
## * Only substitute names you can vouch for. Recording `data = df |> filter(x)`
##   as a symbol would be a lie: re-evaluating it need not give the same rows.
## * rlang::exec(f, !!!args) calls a function from a list without the inlining,
##   if you do not need $call to be meaningful at all.
## * do.call(..., quote = TRUE) is a different thing: it stops the arguments
##   being evaluated, it does not stop them being embedded.

sessionInfo()

r memory do.call