Bug 17378 - deparse(x, control="all") worse than deparse(x) when x contains formula
Summary: deparse(x, control="all") worse than deparse(x) when x contains formula
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Low-level (show other bugs)
Version: R-devel (trunk)
Hardware: All All
: P5 normal
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2018-01-18 16:35 UTC by Martin Maechler
Modified: 2018-02-03 13:55 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Maechler 2018-01-18 16:35:33 UTC
In current R-devel and all previous R versions

fl <- list(ff = y ~ f(x))
(dd <- deparse(fl))                # list(ff = y ~ f(x))
(dA <- deparse(fl, control="all")) # list(ff = quote(y ~ f(x)))
## plus 'Warning message:'
## In deparse(fl, control = "all") : deparse may be incomplete

identical(fl, eval(parse(text = dd))) # TRUE
identical(fl, eval(parse(text = dA))) # FALSE --- even though we say in  ?deparse

>   Using ‘control = "all"’ comes closest to making ‘deparse()’ an
>   inverse of ‘parse()’.  .....

where the  .... mentions that it does not always work.
But clearly,  control = "all" should be uniformly not-worse than the default deparse options.  And here it is worse.
-------

I'm arguing that the documentation should be made correct and hence the code should be changed.

I'm proposing that the "quoteExpressions" deparse options (which is part of "all") should ensure be disabled for formulas.

Martin
Comment 1 Suharto Anggono 2018-02-03 13:55:57 UTC
Never using quote() in deparsing a call to '~' doesn't always give a better result than before. For example, when the object being deparsed is really list(ff = quote(y ~ f(x))) . But yes, after the change, control = "all" is not worse than default, because both give the same result.

y ~ f(x)
is
quote(y ~ f(x))
with addition of "class" and ".Environment" attributes.

The representation of formula from deparse(control = "all") (before change) is incomplete because 'deparse' never includes attributes of a call. Even if attributes were included, environment is not deparsed properly.

y ~ f(x)
can be represented as
structure(quote(y ~ f(x)), class = "formula", .Environment = .GlobalEnv)

quote(y ~ f(x))
can be represented in expanded form as
call("~", as.name("y"), call("f", as.name("x")))