数据。从未打印的函数内部分配:=的表对象

时间:2022-10-25 07:43:31

I would like to modify a data.table within a function. If I use the := feature within the function, the result is only printed for the second call.

我想修改一个数据。表内的一个函数。如果我在函数中使用:=特性,那么结果只会输出到第二个调用。

Look at the following illustration:

请看下图:

library(data.table)
mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
    dt[, z := y - x]
    dt
}

When I call only the function, the table is not printed (which is the standard behaviour. However, if I save the returned data.table into a new object, it is not printed at the first call, only for the second one.

当我只调用函数时,不会打印表(这是标准行为)。但是,如果我保存返回的数据。表进入一个新对象,它不是在第一次调用时打印的,只在第二个调用中打印。

myfunction(mydt)  # nothing is printed   
result <- myfunction(mydt) 
result  # nothing is printed
result  # for the second time, the result is printed
mydt                                                                     
#    x y z
# 1: 1 5 4
# 2: 2 6 4
# 3: 3 7 4 

Could you explain why this happens and how to prevent it?

你能解释一下为什么会发生这种情况以及如何预防吗?

2 个解决方案

#1


30  

As David Arenburg mentions in a comment, the answer can be found here. There was a bug fixed in the version 1.9.6 but the fix introduced this downside.

正如David Arenburg在评论中提到的,答案可以在这里找到。版本1.9.6中修复了一个错误,但是修复带来了这个缺点。

One should call DT[] at the end of the function to prevent this behaviour.

应该在函数的末尾调用DT[]来防止这种行为。

myfunction <- function(dt) {
    dt[, z := y - x][]
}
myfunction(mydt)  # prints immediately
#    x y z
# 1: 1 5 4
# 2: 2 6 4
# 3: 3 7 4 

#2


0  

I'm sorry if I'm not supposed to post something here that's not an answer, but my post is too long for a comment.

我很抱歉,如果我不应该在这里发布一些不是答案的东西,但是我的帖子太长了,不适合评论。

I'd like to point out that janosdivenyi's solution of adding a trailing [] to dt does not always give the expected results (even when using data.table 1.9.6 or 1.10.4) as I do below.

我想指出的是,janosdivenyi在dt中添加末尾[]的解决方案并不总是给出预期的结果(即使使用数据时也是如此)。表1.9.6或1.10.4)如下所示。

The examples below show that if dt is the last line in the function one gets the desired behaviour without the presence of the trailing [], but if dt is not on the last line in the function then a trailing [] is needed to get the desired behaviour.

下面的示例显示,如果dt是函数中的最后一行,那么在不存在结尾[]的情况下就可以得到所需的行为,但是如果dt不在函数的最后一行,则需要一个结尾[]来获得所需的行为。

The first example shows that with no trailing [] on dt we get the expected behaviour when dt is on the last line of the function

第一个例子表明,在没有跟踪[]的情况下,当dt位于函数的最后一行时,我们会得到期望的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  df <- 1
  dt[, z := y - x]
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

Adding a trailing [] on dt gives unexpected behaviour

在dt上添加一个尾随[]会产生意想不到的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  df <- 1
  dt[, z := y - x][]
}

myfunction(mydt)  # Content printed unexpectedly
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

Moving df <- 1 to after the dt with no trailing [] gives unexpected behaviour

将df <- 1移动到没有尾随[]的dt之后会产生意外的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  dt[, z := y - x]
  df <- 1
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Nothing printed unexpectedly

Moving df <- 1 after the dt with a trailing [] gives the expected behaviour

将df <- 1移动到带有结尾[]的dt之后,会得到预期的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  dt[, z := y - x][]
  df <- 1
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

#1


30  

As David Arenburg mentions in a comment, the answer can be found here. There was a bug fixed in the version 1.9.6 but the fix introduced this downside.

正如David Arenburg在评论中提到的,答案可以在这里找到。版本1.9.6中修复了一个错误,但是修复带来了这个缺点。

One should call DT[] at the end of the function to prevent this behaviour.

应该在函数的末尾调用DT[]来防止这种行为。

myfunction <- function(dt) {
    dt[, z := y - x][]
}
myfunction(mydt)  # prints immediately
#    x y z
# 1: 1 5 4
# 2: 2 6 4
# 3: 3 7 4 

#2


0  

I'm sorry if I'm not supposed to post something here that's not an answer, but my post is too long for a comment.

我很抱歉,如果我不应该在这里发布一些不是答案的东西,但是我的帖子太长了,不适合评论。

I'd like to point out that janosdivenyi's solution of adding a trailing [] to dt does not always give the expected results (even when using data.table 1.9.6 or 1.10.4) as I do below.

我想指出的是,janosdivenyi在dt中添加末尾[]的解决方案并不总是给出预期的结果(即使使用数据时也是如此)。表1.9.6或1.10.4)如下所示。

The examples below show that if dt is the last line in the function one gets the desired behaviour without the presence of the trailing [], but if dt is not on the last line in the function then a trailing [] is needed to get the desired behaviour.

下面的示例显示,如果dt是函数中的最后一行,那么在不存在结尾[]的情况下就可以得到所需的行为,但是如果dt不在函数的最后一行,则需要一个结尾[]来获得所需的行为。

The first example shows that with no trailing [] on dt we get the expected behaviour when dt is on the last line of the function

第一个例子表明,在没有跟踪[]的情况下,当dt位于函数的最后一行时,我们会得到期望的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  df <- 1
  dt[, z := y - x]
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

Adding a trailing [] on dt gives unexpected behaviour

在dt上添加一个尾随[]会产生意想不到的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  df <- 1
  dt[, z := y - x][]
}

myfunction(mydt)  # Content printed unexpectedly
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4

Moving df <- 1 to after the dt with no trailing [] gives unexpected behaviour

将df <- 1移动到没有尾随[]的dt之后会产生意外的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  dt[, z := y - x]
  df <- 1
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Nothing printed unexpectedly

Moving df <- 1 after the dt with a trailing [] gives the expected behaviour

将df <- 1移动到带有结尾[]的dt之后,会得到预期的行为

mydt <- data.table(x = 1:3, y = 5:7)

myfunction <- function(dt) {
  dt[, z := y - x][]
  df <- 1
}

myfunction(mydt)  # Nothing printed as expected

mydt  # Content printed as desired
##    x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4