Data.table: Opérateur d'affectation := modifie le corps d'une fonction wrapper

Créé le 18 sept. 2019  ·  4Commentaires  ·  Source: Rdatatable/data.table

Bonjour!

Exemple minimal reproductible :

library(data.table)

f = function(flag = FALSE) {
  dt1 = data.table(a = 1)
  dt2 = data.table(a = 1)
  dt3 = dt1[, .(a, b = 0)]

  print(body(f))
  if (flag) dt3[dt2, b := 999, on = "a"]
  print(body(f))

  return(invisible(dt3))
}

f(flag = FALSE)
f(flag = TRUE)

> f(flag = FALSE)
{
    dt1 = data.table(a = 1)
    dt2 = data.table(a = 1)
    dt3 = dt1[, .(a, b = 0)]
    print(body(f))
    if (flag) 
        dt3[dt2, `:=`(b, 999), on = "a"]
    print(body(f))
    return(invisible(dt3))
}
{
    dt1 = data.table(a = 1)
    dt2 = data.table(a = 1)
    dt3 = dt1[, .(a, b = 0)]
    print(body(f))
    if (flag) 
        dt3[dt2, `:=`(b, 999), on = "a"]
    print(body(f))
    return(invisible(dt3))
}

> f(flag = TRUE)
{
    dt1 = data.table(a = 1)
    dt2 = data.table(a = 1)
    dt3 = dt1[, .(a, b = 0)]
    print(body(f))
    if (flag) 
        dt3[dt2, `:=`(b, 999), on = "a"]
    print(body(f))
    return(invisible(dt3))
}
{
    dt1 = data.table(a = 1)
    dt2 = data.table(a = 1)
    dt3 = dt1[, .(a, b = 999)]
    print(body(f))
    if (flag) 
        dt3[dt2, `:=`(b, 999), on = "a"]
    print(body(f))
    return(invisible(dt3))
}

Comme vous pouvez le voir, après avoir attribué (:=) une nouvelle valeur à la colonne "b" (lorsque l'indicateur est VRAI), le corps de la fonction "f" est modifié sur la ligne "dt3 = ..." ("b = 0 " est remplacé par "b = 999"). Le résultat de l'exécution de la fonction sera alors différent lors de son exécution avec la valeur du drapeau FALSE.

Sortie de sessionInfo() :

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7600)

Matrix products: default

locale:
[1] LC_COLLATE=Russian_Russia.1251  LC_CTYPE=Russian_Russia.1251    LC_MONETARY=Russian_Russia.1251
[4] LC_NUMERIC=C                    LC_TIME=Russian_Russia.1251    

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

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

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0
High bug

Commentaire le plus utile

@mattdowle , merci !

Après d'autres investigations, il s'avère qu'il n'y a pas besoin d'une fonction wrapper.
Voici un exemple concis pour ce problème :

library(data.table)
value = 0
dt1 = data.table(a = 1)
dt2 = dt1[, .(a, b = ..value)]
dt2[1, b := 999]
print(value)

Tous les 4 commentaires

Merci pour le super reportage ! Je peux aussi reproduire en v1.12.2 aussi.

@mattdowle , merci !

Après d'autres investigations, il s'avère qu'il n'y a pas besoin d'une fonction wrapper.
Voici un exemple concis pour ce problème :

library(data.table)
value = 0
dt1 = data.table(a = 1)
dt2 = dt1[, .(a, b = ..value)]
dt2[1, b := 999]
print(value)

Merci. J'ai trouvé la ligne fautive hier et je me suis demandé si ce n'était peut-être pas uniquement une enveloppe. Donc c'est vraiment bien d'avoir déjà un exemple. WIP...

Connexe #3766. Merci @kirillmayantsev de ne pas nous abandonner !

Cette page vous a été utile?
0 / 5 - 0 notes