[R] Printing left-justified character strings

zListserv z||@t@erv @end|ng |rom gm@||@com
Wed Jun 6 13:24:03 CEST 2018


Duncan

Many thanks.  I removed the (re-)definitions for print and print.default, and I redefined print.data.frame using 'x' instead of 'df'.

Your point about possible issues downstream with row names is well taken.  I'll keep a lookout for any untoward side effects.

In the meantime, all is well and I'm grateful for your help.

> On 2018-06-06, at 07:06, Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
> 
> On 06/06/2018 6:28 AM, zListserv wrote:
>> Sorry.  Here's how I re-defined print, print.default, and print.data.frame:
>> print = function(df, ..., right=FALSE, row.names=FALSE) base::print(df, ..., right=right, row.names=row.names)
> 
> base::print doesn't have those arguments.  It only has arguments print(x, ...).  You shouldn't redefine it, since it just dispatches to one of the methods.
> 
> In fact, I think this redefinition is causing the problem way down below:  instead of your two methods applying to the base package generic, they are applying only to your own generic defined here. Auto-printing uses the base generic.
> 
>> print.default = function(df, ..., right=FALSE, row.names=FALSE) base::print.default(df, ..., right=right, row.names=row.names)
> 
> base::print.default doesn't have a row.names argument.  It won't cause an error, but will be ignored.  It already has `right=FALSE` as a default, so it seems pretty pointless to redefine it.
> 
>> print.data.frame = function(df, ..., right=FALSE, row.names=FALSE) base::print.data.frame(df, ..., right=right, row.names=row.names)
> 
> That definition makes sense if you want left justification and no row names, but remember that some print methods may rely on the display of row names for sensible output.  (I can't think of any examples right now, but I'd look at print methods for summary objects if I was searching for them.  There are several that rely on row names when they print matrices, e.g. print.summary.lm.)
> 
> And as a general rule, you should use the same argument names as in the generic, i.e. x instead of df.  It's pretty rare, but someone might say
> print(x = data.frame(1:10)), and your print.data.frame method would absorb the argument into the ... , yielding an error
> 
> 'argument "df" is missing, with no default'
> 
> <snip>




More information about the R-help mailing list