R’s sys.call vs match.call

The sys.call() function returns the exact call to the function.

f <- function(x, y = 2) {
  print(paste("sys.call():", deparse(sys.call())))
  print(paste("match.call():", deparse(match.call())))
  invisible(TRUE)
}

The match.call() function names all arguments and orders them as appropriate.

This results in:

> f(1, 2)
[1] "sys.call(): f(1, 2)"
[1] "match.call(): f(x = 1, y = 2)"
> f(y = 2, x = 1)
[1] "sys.call(): f(y = 2, x = 1)"
[1] "match.call(): f(x = 1, y = 2)"
> f(6)
[1] "sys.call(): f(6)"
[1] "match.call(): f(x = 6)"

Note the last example does not include a y = 2 argument.

Home | Back to blog

This work is licensed under CC BY-NC 4.0 Creative Commons BY-NC image