[R] Environmental oddity.
Ivan Krylov
kry|ov@r00t @end|ng |rom gm@||@com
Sun Nov 7 09:18:28 CET 2021
On Sun, 7 Nov 2021 09:02:36 +0530
Deepayan Sarkar <deepayan.sarkar using gmail.com> wrote:
> This sounds like a difference in precedence. The expression
>
> if (log) 1 else dnorm(x, mean, sd) / sd
>
> is apparently being interpreted differently as
>
> d1: (if (log) 1 else dnorm(x, mean, sd)) / sd
> d2: if (log) 1 else (dnorm(x, mean, sd)) / sd)
>
> It's unclear how environments could affect this, so it would be very
> helpful to have a reproducible example.
This seems to be caused by the deparser producing the same source text
for different expressions:
( x <- expression(`/`(`*`(a, if (b) c else d), e)) )
# expression(a * if (b) c else d/e)
( y <- expression(a * if (b) c else d/e) )
# expression(a * if (b) c else d/e)
all.equal(x, y)
# [1] TRUE
The expressions *seem* to be the same, but:
as.list(x[[1]])
# [[1]]
# `/`
#
# [[2]]
# a * if (b) c else d
#
# [[3]]
# e
as.list(y[[1]])
# [[1]]
# `*`
#
# [[2]]
# a
#
# [[3]]
# if (b) c else d/e
Perhaps it could be possible to make the deparser output extra
parentheses at the cost of slightly uglier output in cases when they
are not needed. all.equal.language uses deparse(), so it will behave
correctly when the deparse() output is fixed.
In the original example, as.list(body(d1)) and as.list(body(d2)) should
show different results, too.
--
Best regards,
Ivan
More information about the R-help
mailing list