You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Print method for subclass makes := return visible output #3029
I'm extending data.table class and I'm having a problem. When defining a custom print method operations with := print the object instead of returning it invisibly.
library(data.table)
dt<- data.table(x=1)
dt[, y:=1] # No output. Correct!
setattr(dt, "class", c("someclass", class(dt)))
dt[, y:=1] # still no output. Yey!print.someclass<-function(x, ...) {
NextMethod("print")
# maybe other things
}
dt[, y:=1] # there's output, Noooo!
(The output doesn't seem to appear when reprexed, but it's there in interactive mode, please test)
I don't know what I'm doing wrong and I cannot find much documentation on this.
First thing that comes to mind is to look for NextMethod in SYS in addition to the mimicsAutoPrint... Or is there some way for downstream users to expand mimicsAutoPrint (on their own? Through the Issues tracker?)
First thing that comes to mind is to look for NextMethod in SYS in addition to the mimicsAutoPrint... Or is there some way for downstream users to expand mimicsAutoPrint (on their own? Through the Issues tracker?)
I don't think that expanding mimicsAutoPrint with a curated list for every custom printing is suitable in the long run. Looking for NextMethod on second to last position in addition with length(class(dt)) seems like a viable approach to me
That identical() condition looks pretty good to me as a test of "auto-printed in interactive session" -- in particular explicit print() does not pass that, as sys.calls() looks like so in that case:
Another idea is to add an argument to print.data.table() like suppressPrint=FALSE that downstreams could run to force print suppression, sent along through NextMethod().
print(as.data.table(mtcars)[,y:=2]) is equivalent to as.data.table(mtcars)[,y:=2], since results are printed implicitly in the console, right? Since the expected behaviour is for := to suppress printing, then, yeah, it is intended.
The idea in #6631 is that what R does in order to auto-print as.data.table(mtcars)[,y:=2] is not exactly print(as.data.table(mtcars)[,y:=2]), but more like eval(substitute(print(x), list(print = base::print)), list(x = previously_obtained_value), globalenv()), and having the contents of base::print in the call stack instead of just quote(print) can be detected. The exact code is from R-3.5...3.6, but the behaviour before that was the same and dates back to R-3.2.0, so we should be fine depending on R ≥ 3.3.
Activity
MichaelChirico commentedon May 2, 2019
@mattdowle I don't quite get what all is meant to be covered here:
First thing that comes to mind is to look for
NextMethodinSYSin addition to themimicsAutoPrint... Or is there some way for downstream users to expandmimicsAutoPrint(on their own? Through the Issues tracker?)nikosbosse commentedon Oct 20, 2024
We're seeing a similar issue in our R scoringutils. Is there any solution or steps we could take? Thank you!
ben-schwen commentedon Oct 21, 2024
I don't think that expanding
mimicsAutoPrintwith a curated list for every custom printing is suitable in the long run. Looking forNextMethodon second to last position in addition withlength(class(dt))seems like a viable approach to meMichaelChirico commentedon Dec 4, 2024
Still feels pretty clunky to me -- does that distinguish explicit
print(dt)on a class extension?edit: no, it won't, c.f.:
It looks like the key to detecting the autoprint is this esoteric first entry in the call stack:
That's just the
print()generic being invoked in asubstitute()-y way:That
identical()condition looks pretty good to me as a test of "auto-printed in interactive session" -- in particular explicitprint()does not pass that, assys.calls()looks like so in that case:Another idea is to add an argument to
print.data.table()likesuppressPrint=FALSEthat downstreams could run to force print suppression, sent along throughNextMethod().MichaelChirico commentedon Dec 4, 2024
Hmm, is it considered intentional behavior that
Does not print anything? I would guess no?
eliocamp commentedon Dec 4, 2024
print(as.data.table(mtcars)[,y:=2])is equivalent toas.data.table(mtcars)[,y:=2], since results are printed implicitly in the console, right? Since the expected behaviour is for:=to suppress printing, then, yeah, it is intended.aitap commentedon Dec 4, 2024
The idea in #6631 is that what R does in order to auto-print
as.data.table(mtcars)[,y:=2]is not exactlyprint(as.data.table(mtcars)[,y:=2]), but more likeeval(substitute(print(x), list(print = base::print)), list(x = previously_obtained_value), globalenv()), and having the contents ofbase::printin the call stack instead of justquote(print)can be detected. The exact code is from R-3.5...3.6, but the behaviour before that was the same and dates back to R-3.2.0, so we should be fine depending on R ≥ 3.3.(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…
(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…