[datatable-help] Override the data.frame print function

Matthew Dowle mdowle at mdowle.plus.com
Mon Nov 7 20:26:29 CET 2011


On Mon, 2011-11-07 at 10:47 -0600, Branson Owen wrote:
> 
> 
>         On Fri, Nov 4, 2011 at 9:15 AM, Chris Neff <caneff at gmail.com>
>         wrote:
>         > I'd rather have the print.data.table function override the
>         > print.data.frame one. How can I do this?
>         
>         Try:
>         
>         print.data.frame <- data.table:::print.data.table
> 
> 
> Does it create a copy of 'print.data.frame' in global environment?

A copy of print.data.table, yes. It might be a copy-on-write, not sure.

> 
> > base:::print.data.frame <- data.table:::print.data.table

That doesn't work, does it?

> Is overwriting base better?

It isn't considered good practice to overwrite base. Protections were
put in place in 2.14.0 and more added in r-devel since. If packages
start doing that, and in different orders, it'll end up a horrible mess.
Having said that ... close your eyes ... FAQ 2.23 documents why
data.table does. In a very careful way (dynamic read, add one line, and
assign back to base), to two functions only, as a last resort. If you've
seen that in .onAttach, it isn't meant to be a demonstration of what is
possible and ok!

> If print.data.frame is always called in global environment, I guess I
> worried too much.

Are you thinking about speed here?  .GlobalEnv is always pos 1 on
search() so those functions are found quickest. base is last on search()
so they're actually slower if repeated many times in a loop, due to
searching() for them.  I think compile()'d code might save the location
in the byte code, to save the searching()ing, not sure.

> 
> 
> Best regards,
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help




More information about the datatable-help mailing list