From murdoch@dunc@n @end|ng |rom gm@||@com  Sat May  6 19:12:09 2023
From: murdoch@dunc@n @end|ng |rom gm@||@com (Duncan Murdoch)
Date: Sat, 6 May 2023 13:12:09 -0400
Subject: [Rd] Change DEFAULTDEPARSE to DEFAULTDEPARSE | SHOWATTRIBUTES ?
In-Reply-To: <fdc5b57c-b401-38df-1090-2109080eb98f@gmail.com>
References: <fdc5b57c-b401-38df-1090-2109080eb98f@gmail.com>
Message-ID: <d2880934-7914-3af2-dd1c-56eea5825d50@gmail.com>

On 06/05/2023 12:26 p.m., Mikael Jagan wrote:
> The deparse options used by default by 'deparse' and 'dput' are
> 
>       c("keepNA", "keepInteger", "niceNames", "showAttributes")
> 
> but Defn.h still has
> 
>       #define DEFAULTDEPARSE		1089 /* KEEPINTEGER | KEEPNA | NICE_NAMES, used for
> calls */
> 
> i.e., with the SHOWATTRIBUTES bit turned off.  Is that on purpose?
> Note that this leads to weird things like:
> 
>       > (expr <- call("is.matrix", matrix(1:4, 2L, 2L)))
>       is.matrix(1:4)
>       > eval(expr)
>       [1] TRUE
> 
> which can confuse anyone not paying close attention ...

I agree that deparse does a better job in this case, but I'm not sure 
I'd recommend the change.  You should try it, and see if any tests are 
broken.  I'd guess there will be some in base R, but I might be wrong.

Contributed packages are another issue.  Lots of packages test for 
changes in their output; this change could break those.

I think the underlying issue is that call("is.matrix", matrix(1:4, 2L, 
2L)) produces something that would never be produced by the parser, so 
"deparsing" isn't really well defined.

For example deparse(expr) also gets it wrong:

   [1] "is.matrix(structure(1:4, dim = c(2L, 2L)))"

Even though that evaluates to the same result, it isn't the expression I 
put into expr.  There are also many examples where you don't get the 
right answer from either version.  A simple one is this:

    > (expr <- call("identity", pi))
    identity(3.14159265358979)
    > eval(expr)
    [1] 3.141593
    > eval(expr) == identity(3.14159265358979)
    [1] FALSE

Here the issue is that the deparsed expression doesn't include the full 
precision for pi that is stored in expr.

(Aside:  This is one reason why it's such a bad idea to use the common 
pattern:

    deparse expression
    modify part of it
    parse the result

You can often get more changes than you intended.  It's better to work 
on the expression directly.)

BTW, I just noticed something else in deparse() that's probably a bug.

    > deparse(expr, control = "exact")
    [1] "quote(I(0x1.921fb54442d18p+1))"

I don't know why "quote" is now showing up; it means that

    parse(text = deparse(expr, control = "exact"))

produces something that's really quite different from expr.

Duncan Murdoch



[Rd] Change DEFAULTDEPARSE to DEFAULTDEPARSE | SHOWATTRIBUTES ?
Duncan Murdoch