1

I noticed this slightly odd behavior.

I've created a data.table by stringing together multiple blocks of [i, j, by] operations, and in the last one I'm using the := assignment operator. But now that data.table doesn't print to console, unless you nudge it to do so with []

Example:

set.seed(1)
raw_dt <- data.table(x = sample(letters[1:5], 50, replace=T))
new_dt <- raw_dt[, .N, by = x][, label := paste(x, 'occurs', N, 'times')]

# this does not print to console
new_dt

# although this does
new_dt[]

It's good that := operations don't print, so you can use them silently in markdown chunks, but I would have expected a new DT assigned like the one above to still print.

[] is a fine workaround, but is this intended behaviour? Any way to get new_dt above to print when invoked by itself, without []?

arvi1000
  • 9,393
  • 2
  • 42
  • 52
  • 1
    Aha, yup. Thank you. My search didn't turn up that answer – arvi1000 Nov 17 '17 at 18:02
  • 1
    Np, btw I see it's also now in the FAQ as "Why do I have to type DT sometimes twice after using := to print the result to console?" https://cran.r-project.org/web/packages/data.table/vignettes/datatable-faq.html – Frank Nov 17 '17 at 18:04
  • 1
    Frequently asked indeed. I'm guessing i didn't find the posts because of how `:=` is treated in SO search (e.g., nothing at https://stackoverflow.com/search?q=data.table+print+%3A%3D ) – arvi1000 Nov 17 '17 at 19:43
  • If you prefer to always print: https://stackoverflow.com/questions/39603651/an-option-to-not-suppress-output-after-assignment-in-data-table – Clayton Stanley Nov 17 '17 at 21:32

0 Answers0