[R] Trying to learn how to write an "advanced" function

Greg Snow 538280 @end|ng |rom gm@||@com
Thu Mar 16 16:12:08 CET 2023


The first thing to understand is that despite similarity in names,
`match` and `match.call` are doing very different things, which should
not be confused with each other.

For understanding what a function is doing, it is helpful to watch
what it does at each step.  With functions like `lm` that are built
in, we cannot easily modify the source code with print statements and
the like, then rerun, but the `trace` and `browser` functions are nice
for allowing us to debug these functions. If you run the following
line:

trace(lm, browser, at=7)

Then this will insert a call to `browser` into the `lm` function which
when you next run `lm` will pause at about line 7 (just after the code
that you show) and allow you to examine the values of `mf` and `m` and
any other variables.

Unfortunately just printing `mf` is not super helpful for this, since
it prettys the call, but printing as.list(mf) is more useful.
Basically mf will hold information on how `lm` was called, the first
element is that `lm` is the function that was called, then further
elements are the arguments that were passed in.

The line with `match` then identifies which elements of mf correspond
to the list of arguments that we want to keep for the next part
(essentially the next few lines create a call object to the
`model.frame` funcion with the same arguments that you passed to `lm`
but without any arguments not in the short list.

When in `browser` mode you can step through the evaluation of the
function with "s" or "n" and quit everything with "Q" (see ?browser
for details)

Don't forget to call `untrace(lm)` when you are through.

On Thu, Mar 16, 2023 at 6:16 AM Sorkin, John <jsorkin using som.umaryland.edu> wrote:
>
> I am trying to understand how to write an "advanced" function. To do so, I am examining the lm fucnction, a portion of which is pasted below. I am unable to understand what match.call or   match does, and several other parts of lm, even when I read the help page for match.call or match.
> (1) can someone point me to an explanation of match.call or match that can be understood by the uninitiated?
> (2) can someone point me to a document that will help me learn how to write an "advanced" function?
>
> Thank you,
> John
>
> > lm
> function (formula, data, subset, weights, na.action, method = "qr",
>     model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
>     contrasts = NULL, offset, ...)
> {
>     ret.x <- x
>     ret.y <- y
>     cl <- match.call()
>     mf <- match.call(expand.dots = FALSE)
>     m <- match(c("formula", "data", "subset", "weights", "na.action",
>         "offset"), names(mf), 0L)
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 using gmail.com



More information about the R-help mailing list