Skip to content

data.table fails with emmeans objects #6874

Description

@matschmitz

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.table with emmeans objects now produces errors, while equivalent operations with base data.frame succeed.

Reproducible Example:

library(data.table) # v1.17.0
library(emmeans) # v1.10.7

mdl <- lm(dist ~ speed, data = cars)
em_res <- emmeans(mdl, ~ 1)

data.frame(em_res)
#        X1 emmean       SE df lower.CL upper.CL
# 1 overall  42.98 2.175002 48 38.60687 47.35313

data.table(em_res)
# Error in setDT(ans, key = key) : LENGTH or similar applied to S4 object

as.data.table(em_res)
# Error: C stack usage 15924944 is too close to the limit

Session Info:

R version 4.4.3 (2025-02-28 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8  
[2] LC_CTYPE=English_United States.utf8    
[3] LC_MONETARY=English_United States.utf8 
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United States.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] emmeans_1.10.7-100003 data.table_1.17.0    

loaded via a namespace (and not attached):
 [1] codetools_0.2-20   multcomp_1.4-28    Matrix_1.7-3       
 [4] lattice_0.22-6     coda_0.19-4.1      TH.data_1.1-3      
 [7] splines_4.4.3      estimability_1.5.1 zoo_1.8-13         
[10] mvtnorm_1.3-3      xtable_1.8-4       grid_4.4.3        
[13] sandwich_3.1-1     compiler_4.4.3     tools_4.4.3       
[16] survival_3.8-3     MASS_7.3-65       

Activity

  1. 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
  2. tdhock commented on Mar 18, 2025

    @tdhock
    Member

    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)
  3. MichaelChirico commented on Mar 18, 2025

    @MichaelChirico
    Member

    Is this a regression?

  4. matschmitz commented on Mar 18, 2025

    @matschmitz
    Author

    It is, yes

  5. added this to the 1.17.2 milestone on Mar 18, 2025
  6. ben-schwen commented on Mar 18, 2025

    @ben-schwen
    Member

    We also get into an infinity recursion with as.data.table(em_res)

  7. MichaelChirico commented on Mar 20, 2025

    @MichaelChirico
    Member

    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

  8. MichaelChirico commented on Mar 20, 2025

    @MichaelChirico
    Member

    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.

  9. rvlenth commented on Mar 20, 2025

    @rvlenth

    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 from data.frame, that you use data.frame() rather than as.data.frame().

    By the way, this is by no means anything new in emmeans. The as.data.frame code there has been the same for years.

  10. rvlenth commented on Mar 20, 2025

    @rvlenth

    Would it help if I add an as.data.table method to the emmeans package?

    Note: I just updated it, so it will take a while for it to reach CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions