This is (likely) because of source references. If you look at
attributes(x[[3]]), you can see a source reference, which contains a source
file, which is an environment that will not be identical when unserialized
and reserialized (there are exceptions, such as globalenv(), but those
aren't source files so aren't relevant here). Additionally, x[[4]] is a
source reference, so another reason it's not identical.
You can use utils::removeSource(x) to fix this issue, removing the source
references, and then they'll be identical.
On Thu, Nov 20, 2025, 13:13 Tim Taylor <tim.taylor at
hiddenelephants.co.uk>
wrote:
> Is there a correct way to compare language objects that may have been
> serialized?
> If I run the following in an interactive, but 'vanilla' R, session:
>
> x <- quote(\(){})
> y <- serialize(x, NULL)
> z <- unserialize(y)
> identical(x, z)
>
> Then the resulting answer is FALSE. But I get TRUE if run as
>
> $ R --vanilla -s -e "x <- quote(\(){})
> y <- serialize(x, NULL)
> z <- unserialize(y)
> identical(x, z)"
> [1] TRUE
>
> Is this where I need to start thinking about the refhook argument?
>
> Any help appreciated.
>
> Tim
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]