Printing left-justified character strings

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Printing left-justified character strings

zListserv
Many (most?) R functions print character strings and factor labels right-justified.

print accepts right=FALSE to print character strings left-justified, but neither head nor tail seem to do so, and even print is a little inconsistent depending on whether it's done while knitting.

Is there a way to set left-justification globally so every routine will print character strings left-justified?
______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

Duncan Murdoch-2
On 05/06/2018 10:24 AM, zListserv wrote:
> Many (most?) R functions print character strings and factor labels right-justified.

Could you be more specific?  I see character strings left justified,
e.g. x <- rep(c("a", "ab", "abc"), 7) prints as

   [1] "a"   "ab"  "abc" "a"   "ab"  "abc" "a"
   [8] "ab"  "abc" "a"   "ab"  "abc" "a"   "ab"
  [15] "abc" "a"   "ab"  "abc" "a"   "ab"  "abc"

In a data frame, I do see it right justified:

      x
1    a
2   ab
3  abc
etc.

It is easy to change the printing of data frames:

print.data.frame <- function(x, ..., right = FALSE) {
   base::print.data.frame(x, ..., right = right)
}

 > data.frame(x)
    x
1  a
2  ab
3  abc

Are there other examples you're seeing?

Duncan Murdoch

>
> print accepts right=FALSE to print character strings left-justified, but neither head nor tail seem to do so, and even print is a little inconsistent depending on whether it's done while knitting.
>
> Is there a way to set left-justification globally so every routine will print character strings left-justified?
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

David Carlson
I think the OP does not realize that head() and tail() do not print anything. They extract the first or last values/rows and if they are not assigned to an object, they automatically go to print().

Redefining print.data.frame would also fix that problem.

David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Duncan Murdoch
Sent: Tuesday, June 5, 2018 11:40 AM
To: zListserv <[hidden email]>; [hidden email]
Subject: Re: [R] Printing left-justified character strings

On 05/06/2018 10:24 AM, zListserv wrote:
> Many (most?) R functions print character strings and factor labels right-justified.

Could you be more specific?  I see character strings left justified,
e.g. x <- rep(c("a", "ab", "abc"), 7) prints as

   [1] "a"   "ab"  "abc" "a"   "ab"  "abc" "a"
   [8] "ab"  "abc" "a"   "ab"  "abc" "a"   "ab"
  [15] "abc" "a"   "ab"  "abc" "a"   "ab"  "abc"

In a data frame, I do see it right justified:

      x
1    a
2   ab
3  abc
etc.

It is easy to change the printing of data frames:

print.data.frame <- function(x, ..., right = FALSE) {
   base::print.data.frame(x, ..., right = right)
}

 > data.frame(x)
    x
1  a
2  ab
3  abc

Are there other examples you're seeing?

Duncan Murdoch

>
> print accepts right=FALSE to print character strings left-justified, but neither head nor tail seem to do so, and even print is a little inconsistent depending on whether it's done while knitting.
>
> Is there a way to set left-justification globally so every routine will print character strings left-justified?
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

zListserv
In reply to this post by Duncan Murdoch-2
Duncan et al

I tried to redefine print.data.frame the way you suggested, but I misplaced the ellipsis by putting it at the end of the function definition instead of immediately following the name of the data frame.

Works now.

Thanks!


> On 2018-06-05, at 12:39, Duncan Murdoch <[hidden email]> wrote:
>
> On 05/06/2018 10:24 AM, zListserv wrote:
>> Many (most?) R functions print character strings and factor labels right-justified.
>
> Could you be more specific?  I see character strings left justified, e.g. x <- rep(c("a", "ab", "abc"), 7) prints as
>
>  [1] "a"   "ab"  "abc" "a"   "ab"  "abc" "a"
>  [8] "ab"  "abc" "a"   "ab"  "abc" "a"   "ab"
> [15] "abc" "a"   "ab"  "abc" "a"   "ab"  "abc"
>
> In a data frame, I do see it right justified:
>
>     x
> 1    a
> 2   ab
> 3  abc
> etc.
>
> It is easy to change the printing of data frames:
>
> print.data.frame <- function(x, ..., right = FALSE) {
>  base::print.data.frame(x, ..., right = right)
> }
>
> > data.frame(x)
>   x
> 1  a
> 2  ab
> 3  abc
>
> Are there other examples you're seeing?
>
> Duncan Murdoch
>
>> print accepts right=FALSE to print character strings left-justified, but neither head nor tail seem to do so, and even print is a little inconsistent depending on whether it's done while knitting.
>> Is there a way to set left-justification globally so every routine will print character strings left-justified?
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

zListserv
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?

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 14:49, zListserv <[hidden email]> wrote:
>
> Duncan et al
>
> I tried to redefine print.data.frame the way you suggested, but I misplaced the ellipsis by putting it at the end of the function definition instead of immediately following the name of the data frame.
>
> Works now.
>
> Thanks!
>
>
>> On 2018-06-05, at 12:39, Duncan Murdoch <[hidden email]> wrote:
>>
>> On 05/06/2018 10:24 AM, zListserv wrote:
>>> Many (most?) R functions print character strings and factor labels right-justified.
>>
>> Could you be more specific?  I see character strings left justified, e.g. x <- rep(c("a", "ab", "abc"), 7) prints as
>>
>> [1] "a"   "ab"  "abc" "a"   "ab"  "abc" "a"
>> [8] "ab"  "abc" "a"   "ab"  "abc" "a"   "ab"
>> [15] "abc" "a"   "ab"  "abc" "a"   "ab"  "abc"
>>
>> In a data frame, I do see it right justified:
>>
>>    x
>> 1    a
>> 2   ab
>> 3  abc
>> etc.
>>
>> It is easy to change the printing of data frames:
>>
>> print.data.frame <- function(x, ..., right = FALSE) {
>> base::print.data.frame(x, ..., right = right)
>> }
>>
>>> data.frame(x)
>>  x
>> 1  a
>> 2  ab
>> 3  abc
>>
>> Are there other examples you're seeing?
>>
>> Duncan Murdoch
>>
>>> print accepts right=FALSE to print character strings left-justified, but neither head nor tail seem to do so, and even print is a little inconsistent depending on whether it's done while knitting.
>>> Is there a way to set left-justification globally so every routine will print character strings left-justified?
>>> ______________________________________________
>>> [hidden email] 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.
>>
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

Duncan Murdoch-2
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

zListserv
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 <[hidden email]> 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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

Duncan Murdoch-2
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Printing left-justified character strings

zListserv
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 <[hidden email]> 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>

______________________________________________
[hidden email] 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.