Skip to content

issue with assignment of eval'd object with by= #1857

Open

Description

@franknarf1

I'm creating one function per row based on a "template":

library(data.table)
options(keep.source = FALSE)

u_cdf = function(u, muU, sdU) pnorm(n, muU, sdU)
v_pdf = function(v, muV) dexp(v, 1/muV)

f0 = substitute(function(v) v_pdf * (1 -  u_cdf), list(u_cdf = body(u_cdf), v_pdf = body(v_pdf)))

parnames = 
  setdiff(Reduce(union, lapply(list(u_cdf, v_pdf), function(f) names(formals(f)))), c("u","v"))

DT = data.table(muU = 0:1, sdU = 2:3, muV = 4:5, p = 6:7)

DT[, f :=  {
    f = eval(do.call(substitute, 
        list(f0, c(.SD, list(u = substitute(p - v))))
    ))
    print(f)
    .(.(f))
}, by=1:nrow(DT), .SDcols = parnames]
# function (v) 
# dexp(v, 1/4L) * (1 - pnorm(n, 0L, 2L))
# function (v) 
# dexp(v, 1/5L) * (1 - pnorm(n, 1L, 3L))

That seems fine, but...

# all the functions are the same after assignment
DT$f
# [[1]]
# function (v) 
# dexp(v, 1/5L) * (1 - pnorm(n, 1L, 3L))

# [[2]]
# function (v) 
# dexp(v, 1/5L) * (1 - pnorm(n, 1L, 3L))

# testing by calling each function, since R likes to print funs in an obfuscatory manner
uniqueN(sapply(DT$f, function(f) f(10)))
# [1] 1

For now I'm doing a for loop instead since speed is not a big issue here and I am sick of debugging environments/evaluations/substitutions. Thanks.

Activity

  1. jangorecki commented on Mar 22, 2020

    @jangorecki
    Member

    Results from the last group overrides the previously computed results. Adding copy does not seem to solve the problem in this case.

  2. vh-d commented on Sep 24, 2021

    @vh-d

    I am facing a similar issue.

    library(data.table)
    
    DT <- data.table(id = letters[1:5], value = 1:5)
    
    create <- function(id, data) {
      obj <- new.env()
      list2env(data, obj)
      obj[["id"]] <- id
    
      cat(id, ":\n")
      lapply(obj, print)
      cat("------------\n")
    
      .GlobalEnv[[id]] <- obj
    
      NULL
    }

    When we run the create() inside by=:

    DT[, create(id = .BY[[1]], data = .SD), by = id]

    all seems fine during the execution:

    a :
    [1] "a"
    [1] 1
    ------------
    b :
    [1] "b"
    [1] 2
    ------------
    c :
    [1] "c"
    [1] 3
    ------------
    d :
    [1] "d"
    [1] 4
    ------------
    e :
    [1] "e"
    [1] 5
    ------------

    But its actually all rewritten by the last batch:

    > # unexpected mutation by reference
    > print(a[["id"]])
    [1] "e"
    > print(a[["value"]])
    [1] 5

    A for loop gives expected results:

    # works as expected
    for (i in seq_len(nrow(DT))) {
      create(DT[i, id], DT[i][, .SD])
    }
    print(a[["id"]])
    print(a[["value"]])
    > print(a[["id"]])
    [1] "a"
    > print(a[["value"]])
    [1] 1
    > sessionInfo()
    R version 4.1.1 (2021-08-10)
    Platform: x86_64-pc-linux-gnu (64-bit)
    Running under: KDE neon User Edition 5.22
    
    ...
    
    other attached packages:
    [1] data.table_1.14.0
    ...
  3. added
    programming
    and removed
    programming
    on Jan 7, 2024
  4. added
    programming
    on Jul 14, 2025
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

    programming

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions