[Rd] Change DEFAULTDEPARSE to DEFAULTDEPARSE | SHOWATTRIBUTES ?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat May 6 19:12:09 CEST 2023


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



More information about the R-devel mailing list