Search code examples
rreturndata.table

R function returns nothing instead of data.table object when data.table := is last operation


A function taking a data.table df as input uses the data.table := operation should return the modified data.table but returns nothing even with explicit return(df) statement. The function returns the data.table if we force a transformation to data.table with data.table(df).

What is the reason behind this behavior? And what are the good practices in coding functions with the data.table := operator?

Here is a minimal example:

library(data.table)

data <- data.table(x = 1:3)

test_function_1 <- function(df){
    df[, new_column := 1]
}

test_function_2 <- function(df){
    df[, new_column := 1]
    return(df)
}

test_function_3 <- function(df){
    df[, new_column := 1]
    data.table(df)
}

test_function_1(data) # returns nothing
test_function_2(data) # returns nothing
test_function_3(data) # returns the modified data.table

Here is my sessionInfo() if needed:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)


Matrix products: default

    [...]

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

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

Solution

  • library(data.table)
    
    data <- data.table(x = 1:3)
    
    test_function_1 <- function(df){
        df[, new_column := 1][]
    }
    
    test_function_2 <- function(df){
        df[, new_column := 1][]
        return(df)
    }
    
    test_function_3 <- function(df){
        df[, new_column := 1]
        data.table(df)
    }
    
    test_function_1(data) # returns the modified data.table
    test_function_2(data) # returns the modified data.table
    test_function_3(data) # returns the modified data.table
    

    more info: H E R E