Skip to content
Prev 255644 / 398506 Next

converting "call" objects into character

On Sun, Apr 3, 2011 at 11:42 AM, David Winsemius <dwinsemius at comcast.net> wrote:
If you want to examine a call object you need to ensure that it is not
evaluated.  Evaluating a number or a character string is not a problem
because

eval(4)

is the same as

4

However, evaluating a function call should be different from the call
itself.  As David shows, the str function is careful not to evaluate
the call object.  (Martin and I found ourselves going around in
circles when looking at the structure of a fitted model object that
included a call and he kindly changed the behavior of str().)

So you need to decide when a function, such as print(), evaluates its
arguments or when it doesn't, which can get kind of complicated.  An
alternative is to use match.call() repeatedly instead of trying to
save the value, as in
function(x) {
    print(match.call())
    list(x=x, logf = match.call())
}
fTest(x = 2)
$x
[1] 2

$logf
fTest(x = 2)

The trick there is that the value of match.call() is the unevaluated
call whereas

myCall <- match.call()
print(myCall)

evaluates myCall in the call to print, thereby evaluating the function
fTest again.

Is this sufficiently confusing?  :-)