Repository navigation
data.table fails with emmeans objects #6874
Description
Activity
- changed the title
[-]as.data.table fails with emmeans objects (S4 class) causing stack overflow[/-][+]data.table fails with emmeans objects[/+]on Mar 18, 2025 - addednon-atomic columne.g. list columns, S4 vector columnse.g. list columns, S4 vector columns
on Mar 18, 2025 I can reproduce. There is a simple work around:
> as.data.table(data.frame(em_res)) X1 emmean SE df lower.CL upper.CL <fctr> <num> <num> <num> <num> <num> 1: overall 42.98 2.175002 48 38.60687 47.35313 > data.table(em_res) Erreur dans setDT(ans, key = key) : LENGTH or similar applied to S4 object > traceback() 3: setDT(ans, key = key) 2: as.data.table.list(x, keep.rownames = keep.rownames, check.names = check.names, .named = nd$.named) 1: data.table(em_res)
Is this a regression?
It is, yes
We also get into an infinity recursion with
as.data.table(em_res)Funnily enough they also mention infinite loops for their
as.data.frame()method 🙃https://github.com/rvlenth/emmeans/blob/d8842bef1a8b5c51625076b3f6e8daf27dd3ef91/R/summary.R#L24-L31
I proposed rvlenth/emmeans#525... I suspect this is a downstream bug, but Ben's workaround doesn't hurt. We could look into dropping it again in 1.18.0 to remove the maintenance overhead.
I do not intend to change
emmeans::as.data.frame()-- see my comment in rvlenth/emmeans#525. I suggest that if you wish to discard special properties of a class that inherits fromdata.frame, that you usedata.frame()rather thanas.data.frame().By the way, this is by no means anything new in emmeans. The
as.data.framecode there has been the same for years.Would it help if I add an
as.data.tablemethod to the emmeans package?Note: I just updated it, so it will take a while for it to reach CRAN.
- added a commit that references this issue
on May 22, 2026
This issue is fairly recent and used to work perfectly fine in the past. It may be partly related to the R version.
Using
data.tablewithemmeansobjects now produces errors, while equivalent operations with basedata.framesucceed.Reproducible Example:
Session Info: