{"action":"created","issue":{"url":"https://api.github.com/repos/IRkernel/IRkernel/issues/127","repository_url":"https://api.github.com/repos/IRkernel/IRkernel","labels_url":"https://api.github.com/repos/IRkernel/IRkernel/issues/127/labels{/name}","comments_url":"https://api.github.com/repos/IRkernel/IRkernel/issues/127/comments","events_url":"https://api.github.com/repos/IRkernel/IRkernel/issues/127/events","html_url":"https://github.com/IRkernel/IRkernel/issues/127","id":74377169,"number":127,"title":"Setting data.table column outputs whole table to screen","user":{"login":"abielr","id":168236,"avatar_url":"https://avatars.githubusercontent.com/u/168236?v=3","gravatar_id":"","url":"https://api.github.com/users/abielr","html_url":"https://github.com/abielr","followers_url":"https://api.github.com/users/abielr/followers","following_url":"https://api.github.com/users/abielr/following{/other_user}","gists_url":"https://api.github.com/users/abielr/gists{/gist_id}","starred_url":"https://api.github.com/users/abielr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/abielr/subscriptions","organizations_url":"https://api.github.com/users/abielr/orgs","repos_url":"https://api.github.com/users/abielr/repos","events_url":"https://api.github.com/users/abielr/events{/privacy}","received_events_url":"https://api.github.com/users/abielr/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/IRkernel/IRkernel/labels/upstream%20bug","name":"upstream bug","color":"e11d21"},{"url":"https://api.github.com/repos/IRkernel/IRkernel/labels/%E2%86%92%20repr","name":"→ repr","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":43,"created_at":"2015-05-08T14:15:29Z","updated_at":"2016-05-26T14:37:23Z","closed_at":null,"body":"If I run the code below, the second line will cause the entire `dat` object to be output, whereas at an R console it wouldn't return anything. The syntax used is the special syntax for setting columns with the popular `data.table` package. I'm using the 1.9.5 devel version of `data.table`. \r\n\r\n```\r\nlibrary(data.table)\r\ndat <- data.table(x1=1:10)\r\ndat[, x2 := 1:10]\r\n```"},"comment":{"url":"https://api.github.com/repos/IRkernel/IRkernel/issues/comments/221889710","html_url":"https://github.com/IRkernel/IRkernel/issues/127#issuecomment-221889710","issue_url":"https://api.github.com/repos/IRkernel/IRkernel/issues/127","id":221889710,"user":{"login":"janschulz","id":890156,"avatar_url":"https://avatars.githubusercontent.com/u/890156?v=3","gravatar_id":"","url":"https://api.github.com/users/janschulz","html_url":"https://github.com/janschulz","followers_url":"https://api.github.com/users/janschulz/followers","following_url":"https://api.github.com/users/janschulz/following{/other_user}","gists_url":"https://api.github.com/users/janschulz/gists{/gist_id}","starred_url":"https://api.github.com/users/janschulz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janschulz/subscriptions","organizations_url":"https://api.github.com/users/janschulz/orgs","repos_url":"https://api.github.com/users/janschulz/repos","events_url":"https://api.github.com/users/janschulz/events{/privacy}","received_events_url":"https://api.github.com/users/janschulz/received_events","type":"User","site_admin":false},"created_at":"2016-05-26T14:37:23Z","updated_at":"2016-05-26T14:37:23Z","body":"Ok, I tried this:\r\n\r\n```R\r\nlibrary(data.table)\r\nold = data.table:::mimicsAutoPrint\r\nold\r\nreprs = c(\"repr_text.data.table\", \"repr_latex.data.table\", \"repr_markdown.data.table\", \"repr_html.data.table\", \"repr_text.default\")\r\nassignInNamespace(\"mimicsAutoPrint\", c(old, reprs), \"data.table\")\r\ndat <- data.table(x1=1:10)\r\ndat[, x2 := 1:10]\r\n```\r\n\r\nand it still prints because we do have a `repr_text.data.frame` which is used and prints the `dat` because it is also a `data.frame`. I also tried to remove that method, bu then we ran into this callstack:\r\n\r\n```\r\n[[20]]\r\nwithCallingHandlers(withVisible(value_fun(ev$value, ev$visible)), \r\n    warning = wHandler, error = eHandler, message = mHandler)\r\n\r\n[[21]]\r\nwithVisible(value_fun(ev$value, ev$visible))\r\n\r\n[[22]]\r\nvalue_fun(ev$value, ev$visible)\r\n\r\n[[23]]\r\nvalue_handler(x)\r\n\r\n[[24]]\r\nprepare_mimebundle(obj, .self$handle_display_error)\r\n\r\n[[25]]\r\nrepr_text(obj)\r\n\r\n[[26]]\r\nrepr_text.data.frame(obj)\r\n\r\n[[27]]\r\nNextMethod()\r\n\r\n[[28]]\r\nrepr_text.default(obj)\r\n\r\n[[29]]\r\npaste(utils::capture.output(print(obj)), collapse = \"\\n\")\r\n\r\n[[30]]\r\nutils::capture.output(print(obj))\r\n\r\n[[31]]\r\nevalVis(expr)\r\n\r\n[[32]]\r\nwithVisible(eval(expr, pf))\r\n\r\n[[33]]\r\neval(expr, pf)\r\n\r\n[[34]]\r\neval(expr, envir, enclos)\r\n\r\n[[35]]\r\nprint(obj)\r\n\r\n[[36]]\r\nprint.data.table(obj)\r\n```\r\n\r\nwhich seems not to match `( length(SYS) > 3L &&  as.character(SYS[[length(SYS)-3L]][[1L]]) %chin% mimicsAutoPrint )`\r\n\r\nSo Rdatatable/data.table@689b624 seems to be not working here because teh callstack is just too different to the one from knit_print  :-(\r\n\r\nSo we do have to go the `repr_text.data.table` with `shouldPrint()` way. \r\n\r\n```R\r\nrepr_text.data.table <- function(obj, ...){\r\n    if (!data.table::shouldPrint(obj)) {\r\n        invisible(NULL) # in IRkernel, will prevent any other repr_xx methods from being called\r\n    } else {\r\n        NextMethod() # fallsback to `repr_text.default`, which uses print(obj)\r\n    }\r\n}\r\n# No need for repr_html/... as the only reason we have the above method \r\n# is to return null, which indicates to the IRkernel that nothing else should be printed.\r\n# the shouldPrint() actually resets the flag, so it can't be used twice anyway...\r\n```\r\n\r\nThis works as intended: \r\n\r\n```R\r\ndat <- data.table(x1=1:10)\r\ndat[, x2 := 1:10] # does not print\r\ndat # prints\r\n```\r\n\r\nBut now we have a different problem: \r\n\r\nCurrently we implicitly assume that each `repr_*` is independent of the other, so by calling `shouldPrint()` once in `repr_text.data.table()` we do not prevent printing with the other methods (e.g. a new `repr_html.data.table`) because the flag is reset. The actual situation is a bit different, because when `repr_text` returns an empty string, the [rest of the mimetypes are not called in irkernel](https://github.com/IRkernel/IRkernel/blob/master/R/handlers.r#L8). \r\n\r\nSo maybe we should make this explicit in the documentation of repr? \r\n\r\nThe alternative is adding a `repr_should_represent` method which irkernel could then use and which has a special `repr_should_represent.data.table` which uses the flag.\r\n\r\n@mattdowle: would you be able to include the `repr_text.data.table` function in the `data.table`? That way we do not need to guard against `data.table` being loaded and against older versions of the `data.table` package.\r\n\r\n(In general, we would like to have packages exporting `repr_xxx` implementations for their data structures instead of having these implementations in the `repr` packages for this reasons...)\r\n\r\nAs a bonus, you could probably replace the knit_print specific callstack lookup (`|| ( length(SYS) > 3L &&  as.character(SYS[[length(SYS)-3L]][[1L]]) %chin% mimicsAutoPrint )` with \r\n\r\n```\r\nknit_print.data.table <- function(x, ...) {\r\n    if (!data.table::shouldPrint(x)) {\r\n        invisible(NULL)\r\n    } else {\r\n        NextMethod() # which will fall back to your normal print, which will now see `shouldPrint() == T`\r\n    }\r\n```\r\n\r\nThis would prevent a problem if knitr would ever change it's knit_print.default implementation so that the above callstack would be different."}}
{"actor":{"display_login":"janschulz"}}