On 05/06/2018 7:49 PM, zListserv wrote:> p.s. It seems to work for print command, but not for head, tail, or printing a data frame, per below. Any way fix the others so they all left-justify?You haven't shown us what you did. Duncan Murdoch
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)
print.default = function(df, ..., right=FALSE, row.names=FALSE)
base::print.default(df, ..., right=right, row.names=row.names)
print.data.frame = function(df, ..., right=FALSE, row.names=FALSE)
base::print.data.frame(df, ..., right=right, row.names=row.names)
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
R> x
rep(c("a", "ab", "abc"), 7)
1 a
2 ab
3 abc
4 a
5 ab
6 abc
7 a
8 ab
9 abc
10 a
11 ab
12 abc
13 a
14 ab
15 abc
16 a
17 ab
18 abc
19 a
20 ab
21 abc
> On 2018-06-05, at 20:16, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:
>
> On 05/06/2018 7:49 PM, zListserv wrote:
>> p.s. It seems to work for print command, but not for head, tail, or
printing a data frame, per below. Any way fix the others so they all
left-justify?
>
> You haven't shown us what you did.
>
> Duncan Murdoch
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 abcI 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 abcOr that. Duncan Murdoch