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?
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? > ______________________________________________ > R-help at 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. >
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:r-help-bounces at r-project.org] On Behalf Of Duncan Murdoch Sent: Tuesday, June 5, 2018 11:40 AM To: zListserv <zlistserv at gmail.com>; r-help at r-project.org 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? > ______________________________________________ > R-help at 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. >______________________________________________ R-help at 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.
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 <murdoch.duncan at gmail.com> 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? >> ______________________________________________ >> R-help at 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. >