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.
If you are using v1.9.6, see the corresponding Readme (sec. Bugfixes, 1st entry, https://github.com/Rdatatable/data.table):
if (TRUE) DT[,LHS:=RHS] no longer prints, #869 and #1122. Tests added. To get this to work we've had to live with one downside: if a := is used inside a function with no DT[] before the end of the function, then the next time DT or print(DT) is typed at the prompt, nothing will be printed. A repeated DT or print(DT) will print. To avoid this: include a DT[] after the last := in your function. If that is not possible (e.g., it's not a function you can change) then DT[] at the prompt is guaranteed to print. As before, adding an extra [] on the end of a := query is a recommended idiom to update and then print; e.g. > DT[,foo:=3L][]. Thanks to Jureiss and Jan Gorecki for reporting.
Thus: Does calling DT[]
after your function call help?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With