-2

This is related to data.table objects not printed after returned from function but is not a dupe. This question is to specifically look for a way to not suppress output when using an assignment := data.table line at the repl. So in cases where an eval would cause R to call the print method.

I've upgraded and noticed that := returns invisibly now. As I develop with an interactive workflow this disrupts the flow when I am building a pipeline, add an assignment := line, and then expect to see the results to use as context for adding an additional step.

Sure I can add [] to the assignment lines, but this would be after realizing that my assignment line didn't print (because I didn't do this prior), and so my flow was just disrupted prior to adding this. Or, one might argue that I should always tack on [] with every assignment line so that my flow isn't disrupted, but then I have to remember to do that for only assignment lines, and since a pipeline commonly mixes assignment with filter, in practice this introduces extra cognitive overhead that - to an end user - feels unnecessary and looks inconsistent when going line by line in the pipeline.

I would rather not argue about choosing to return invisibly or not, as I figure this has been thoroughly discussed already, and the correct decision for the majority of people and use cases has been made. Instead, is there an option I can set or workaround to have := not return invisibly?

Community
  • 1
  • 1
Clayton Stanley
  • 7,513
  • 9
  • 32
  • 46
  • 1
    This is a relevant discussion http://stackoverflow.com/questions/15267018/knitr-gets-tricked-by-data-table-assignment – David Arenburg Sep 20 '16 at 21:07
  • 3
    What version did you upgrade from? `:=` has been returning invisibly for many *years* now. Adding `[]` at the end is the easiest option I'm aware of. – eddi Sep 20 '16 at 21:12
  • @Eddie 1.9.6 to 1.9.6 :). I also upgraded R, vim, vim-r, tmux, and I'm on a new machine. So something from my previous setup was actually allowing this behavior to persist for me in 1.9.6. I figured though that since this was a change that was made to data.table, the question might be relevant for others that 'upgraded'. In the meantime, I am systematically trying to see what changed in my setup, so that I might use that for inspiration for a workaround. – Clayton Stanley Sep 20 '16 at 21:38
  • Looks like as a hack I can patch data.table:::shouldPrint – Clayton Stanley Sep 20 '16 at 22:05
  • 1
    To clarify @eddi 's comment, in case you didn't quite get it. You don't need to add a new line with `[]`. You just append it to the same command like `dt[, col := x][] ` – dww Sep 20 '16 at 23:52
  • @dww The flow state is still lost either way – Clayton Stanley Sep 21 '16 at 00:34
  • @DavidArenburg thanks but grepping 1.9.6 I think depthtrigger is no more. However, it did allow me to find the shouldPrint function. 'till tomorrow to see if I can find a way to patch that at runtime. – Clayton Stanley Sep 21 '16 at 00:41
  • 2
    I think you need to add an MRE to demonstrate exactly what you mean by "disrupts the flow". I'm finding it hard to understand what cases would be broken by using `[]` – dww Sep 21 '16 at 01:11
  • @dww Having to use [] creates potential for a post completion error. Post completion errors disrupt the flow. – Clayton Stanley Sep 21 '16 at 01:13
  • See [FAQ 2.23](https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html#why-do-i-have-to-type-dt-sometimes-twice-after-using-to-print-the-result-to-console) – MichaelChirico Sep 22 '16 at 13:51
  • Possible duplicate of [data.table objects not printed after returned from function](http://stackoverflow.com/questions/32988099/data-table-objects-not-printed-after-returned-from-function) – MichaelChirico Sep 22 '16 at 13:54
  • @MichaelChirico related but not a dup. This is to look for a way to have data.table print after assignment := when in a repl (so in cases when the typical print method is called by R after an eval operation). – Clayton Stanley Sep 22 '16 at 15:25

1 Answers1

1

One approach in 1.9.6 is to patch the print.data.table S3 method.

Prior to calling the original method, set the .global$print value to "" (default). This undoes how this value was just changed prior to the generic print method being called (using dynamic scoping rules), in the case where data.table would like to return invisibly (e.g., an assignment := line).

The effect is that the custom print method for data.table is still called, but data.table no longer tries to modify R's default logic to decide when and when not to print.

Likely a naive solution, as I'm still learning about packages, namespaces, environments, S3 methods, etc.

library(data.table)
print.data.table.orig = get('print.data.table', envir=asNamespace('data.table'))
print.data.table.patch = function(x, ...) {
  .globalRef = get('.global', envir=asNamespace('data.table'))
  .globalRef$print = ""
  print.data.table.orig(x, ...)
}

library(R.methodsS3)
setMethodS3('print', 'data.table', print.data.table.patch) 


fTbl = data.table(x=1:500000)
fTbl[, x := 5]
        x
     1: 5
     2: 5
     3: 5
     4: 5
     5: 5
    ---  
499996: 5
499997: 5
499998: 5
499999: 5
500000: 5

fTbl
        x
     1: 5
     2: 5
     3: 5
     4: 5
     5: 5
    ---  
499996: 5
499997: 5
499998: 5
499999: 5
500000: 5
> 
Clayton Stanley
  • 7,513
  • 9
  • 32
  • 46