Let's define a function that modifies a data table in R:
TestFun <- function( dt.in ) {
dt <- copy( dt.in )
dt[ 1, a:=9 ]
return( dt )
}
Let's create a small test table, and run the function on it:
dt.test <- data.table( a = 1:3, b = 1:3 )
TestFun( dt.test )
There is no return value after this, which is already strange given that I have explicitly specified what to return, and I've even copied the data table to avoid issues associated with reference semantics.
But the really strange thing comes after this. Let's store the results:
a <- TestFun( dt.test )
A quick str( a )
confirms that a
indeed stores the (correct) result - it seems the function returned it invisibly, but I don't know why...
But the story is still not over! If I try to print a
...
> a
>
...nothing happens. But if I enter
> a
a b
1: 9 1
2: 2 2
3: 3 3
>
again, it prints the table! It is almost esoteric: I simply typed a
twice, so what I have done was completely identical, yet its behaviour was different! What's going on here...?