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'




and this is what it yields (I would like it to print without row names and with 
text left-adjusted):

R> x <- as.data.frame(rep(c("a", "ab", "abc"), 7))
R> print(x)
rep(c("a", "ab", "abc"), 7)
a
ab
abc
a
ab
abc
a
ab
abc
a
ab
abc
a
ab
abc
a
ab
abc
a
ab
abc
R> head(x)
  rep(c("a", "ab", "abc"), 7)
1                           a
2                          ab
3                         abc
4                           a
5                          ab
6                         abc

I don't get that, because I didn't redefine the generic, only the methods.

R> x
   rep(c("a", "ab", "abc"), 7)
1                            a
2                           ab
3                          abc

Or that.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to