Skip to content

Changes inside a function sometimes leak outside, sometimes not #5330

Description

@zigfried76

Hello
After much work I was able to locate my problem to a small example:

library(data.table)

dat <- data.frame(col1=c(1,2), col2=c(3,4))

f1 <- function(d) { setDT(d); d[TRUE,   col2:=c(5,6)] }
f2 <- function(d) { setDT(d); d[col1<3, col2:=c(5,6)] }

f1(dat)
dat   # not changed:
#    col1 col2
# 1:    1    3
# 2:    2    4

f2(dat)
dat    # changed:
#    col1 col2
# 1:    1    5
# 2:    2    6

I read that data.table operations are by reference, but not sure what that means when a data.frame becomes a data.table inside a function. Are changes to this new born data.table supposed to apply to the external copy or not?
Either way I hope one of those can be done in all cases, otherwise it is very confusing.

Thank you very much for your good package!

# Output of sessionInfo()

sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

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

other attached packages:
[1] data.table_1.14.2

loaded via a namespace (and not attached):
[1] compiler_4.1.2 tools_4.1.2

Activity

  1. MichaelChirico commented on Feb 12, 2022

    @MichaelChirico
    Member

    @OfekShilon could you PTAL? this looks similar to your recent contributions around functions using data.table operations on data.framez

  2. OfekShilon commented on Feb 12, 2022

    @OfekShilon
    Contributor

    @MichaelChirico Thanks for noticing! Just verified that PR #4978 indeed solves this. It's not so recent - 9M+ old... Hopefully it can be merged now.

  3. zigfried76 commented on Feb 12, 2022

    @zigfried76
    Author

    "PR" means there is a fix already on the way? Sorry I am new to github.

  4. MichaelChirico commented on Feb 12, 2022

    @MichaelChirico
    Member

    you can follow the link to see a fix. it's not yet merged -- Matt's OS bandwidth has been pretty limited of late.

    In principle you could install from that branch to fix your issue.

    generally I recommend using as.data.table() inside functions instead of setDT

  5. zigfried76 commented on Feb 12, 2022

    @zigfried76
    Author

    Thank you, I will try to install the fixed version. In my real work the "d" is large so I was hoping not to copy it - but if I won't succeed in installing the fix that is what I will do. Anyway if you're already aware of this issue feel free to close this as a duplicate of #4978 . Thanks!

  6. MichaelChirico commented on Feb 12, 2022

    @MichaelChirico
    Member

    depending on your use case you might also try data.table:::shallow. the usual caution with private functions applies

  7. linked a pull request that will close this issueallow.assign.inplace attribute #4978on Feb 14, 2022
  8. OfekShilon commented on Mar 15, 2022

    @OfekShilon
    Contributor

    @mattdowle Any chance of merging #4978?

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

    by-referenceIssues related to by-reference/copying behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions