in my function, I modify column(s) of a data.table given as argument. I have noticed that if I then try to display this data.table (I mean, just by its name) right after calling the function, nothing happens, and only subsequent calls display the table:
DT <- data.table(colA=1:4, colB=4:1) # sample table
modcol <- function(dtIn){
dtIn[, colA:=NULL];
return(TRUE);
}
DT # display table before any changes:
# colA colB
#1: 1 4
#2: 2 3
#3: 3 2
#4: 4 1
modcol(DT) # run our function
#[1] TRUE
DT # silent!
DT # only second call display modified table
# colB
#1: 4
#2: 3
#3: 2
#4: 1
it only happens when my function returns a value (doesn't matter, by return()
or by invisible()
) and only if table content is modified (if, for example, instead of deleting a column I change columns names - this effect does not occur). This behavior is not really making any problems for me, but I am still curious why does it happen.