Olá!
Exemplo reproduzível mínimo :
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))
}
Como você pode ver, após atribuir (: =) um novo valor à coluna "b" (quando o sinalizador for TRUE), o corpo da função "f" é alterado na linha "dt3 = ..." ("b = 0 "é substituído por" b = 999 "). O resultado da execução da função será diferente ao executá-la com o valor do sinalizador FALSE.
Saída 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
Obrigado pelo excelente relatório! Também posso reproduzir na v1.12.2.
@mattdowle , obrigado!
Após mais investigações, descobriu-se que não há necessidade de uma função de invólucro.
Aqui está um exemplo conciso para este problema:
library(data.table)
value = 0
dt1 = data.table(a = 1)
dt2 = dt1[, .(a, b = ..value)]
dt2[1, b := 999]
print(value)
Obrigado. Achei a linha com defeito ontem e me perguntei se poderia não ser apenas invólucro. Portanto, é muito bom já ter um exemplo. WIP ...
Relacionado # 3766. Obrigado @kirillmayantsev por não desistir de nós!
Comentários muito úteis
@mattdowle , obrigado!
Após mais investigações, descobriu-se que não há necessidade de uma função de invólucro.
Aqui está um exemplo conciso para este problema: