[R] most stable way to output text layout

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Sat Jun 12 20:00:04 CEST 2021


Just FYI, Jeremie, you can do what you want fairly easily if you look at the
options available to print() and sprint().

 

You can ask NA conversion to be done here directly at print time:

 

print(mat, na.print="")

      [,1] [,2] [,3] [,4] [,5] [,6]         [,7] [,8]               [,9]
[,10]

[1,]           "a"            "n_missing:" "0"


 [2,]           "b"            "n_unique:"  "10"


 [3,]           "c"            "freq:"


 [4,]           "d"                         "a"  "b"                "c"
"d"  

 [5,]           "e"                         "1"  "1"                "1"
"1"  

 [6,]           "f"


 [7,]           "g"


 [8,]           "h"                              "best match: [1]:" "foo"


 [9,]           "i"


[10,]           "j"


 

 

But you see to want the columns of constant width, try this:

 

mat <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, "n_missing:", "n_unique:", "freq:", NA, NA, NA, NA, NA, NA, NA, "0",
"10", NA, "a", "1", NA, NA, NA, NA, NA, NA, NA, NA, "b", "1", NA, NA, "best
match: [1]:", NA, NA, NA, NA, NA, "c", "1", NA, NA, "foo", NA, NA, NA, NA,
NA, "d", "1", NA, NA, NA, NA, NA), .Dim = c(10L, 10L))

mat[is.na(mat)] <-""

col_width <- max(unlist(lapply(mat, nchar))) + 1

mat <- sprintf("%-*s", col_width, mat)

print(mat, quote=FALSE)

 

I noted your maximum column need was 16 but set it up to calculate that
dynamically and add one. Then sprint is asked to make all columns that width
and finally print put that matrix out without quotes lie this:

 

  [1]


  [7]


 [13]


 [19]                                     a                 b
c                 d                

 [25] e                 f                 g                 h
i                 j                

 [31]


 [37]


 [43]


 [49]                                     n_missing:        n_unique:
freq:                              

 [55]


 [61] 0                 10                                  a
1                                  

 [67]


 [73]                   b                 1
best match: [1]: 

 [79]
c                

 [85] 1                                                     foo


 [91]                                                       d
1                                  

 [97]


 

I made it  left justified and removing the "-" changes that.

 

You can also, of course, convert the matric to a data.frame or tibble and
play various games along these lines and the n use the ways to print a
data.frame that get you what you need.

 

 

 

 

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeremie Juste
Sent: Saturday, June 12, 2021 12:25 PM
To: r-help using r-project.org
Subject: [R] most stable way to output text layout

 

Hello,

 

I'm trying to print a razor thin front-end using just text matrices and the
command prompt.

 

I admit that it is a bit crazy, it seems to do the job and is very quick to
implement...  Except that I don't know of to fix the layout.

 

I'm just seeking to map column names to a standard domain in an interactive
way.

 

For instance, at one iteration, the following matrix is produced:

 

mat <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, "n_missing:", "n_unique:", "freq:", NA, NA, NA, NA, NA, NA, NA, "0",
"10", NA, "a", "1", NA, NA, NA, NA, NA, NA, NA, NA, "b", "1", NA, NA, "best
match: [1]:", NA, NA, NA, NA, NA, "c", "1", NA, NA, "foo", NA, NA, NA, NA,
NA, "d", "1", NA, NA, NA, NA, NA), .Dim = c(10L, 10L))

 

 

which I represent in the console using the following command

 

 

    apply(

      mat,1,

      function(x) {

        x[is.na(x)] <-""

        cat(x,"\n")

      })

 

Do you have any suggestion for how can I have better control on the print
layout of the  matrix so that I can fix the width of each cell?

 

Best regards,

--

Jeremie Juste

 

______________________________________________

 <mailto:R-help using r-project.org> R-help using r-project.org mailing list -- To
UNSUBSCRIBE and more, see  <https://stat.ethz.ch/mailman/listinfo/r-help>
https://stat.ethz.ch/mailman/listinfo/r-help

PLEASE do read the posting guide
<http://www.R-project.org/posting-guide.html>
http://www.R-project.org/posting-guide.html

and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]



More information about the R-help mailing list