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
Can't say this appeals to me, but sprintf would make a difference: apply( mat,1, function(x) { x[is.na(x)] <-"" cat(paste(sprintf("%16s",x)),"\n") }) On June 12, 2021 9:24:51 AM PDT, Jeremie Juste <jeremiejuste at gmail.com> wrote:>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,-- Sent from my phone. Please excuse my brevity.
Jeremie, i'm not totally sure i understand your desire. but, does this help? ---- mx <- max(nchar(mat), na.rm=TRUE) apply( mat,1, function(x) { x[is.na(x)] <-"" cat(sprintf("%*s", mx, x), "\n") }) ---- cheers, Greg
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 at r-project.org> On Behalf Of Jeremie Juste Sent: Saturday, June 12, 2021 12:25 PM To: r-help at 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 at r-project.org> R-help at 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]]
Hi Jeremie, Or assuming that the matrix will always contain strings: tabify<-function(x,col_names=NULL) { # convert NAs to "NA" x[is.na(x)]<-"NA" # if this matrix doesn't have any column names if(is.null(col_names)) col_names<-LETTERS[1:ncol(x)] # get the format argument for sprintf tab_width<-max(nchar(c(col_names,x)))+1 # display the column names cat(format(col_names,width=tab_width),"\n") # display the matrix apply(x,1,function(x) cat(format(x,width=tab_width),"\n")) } tabify(mat) mat_names<-c("first","second","third","fourth","fifth","sixth","seventh", "eighth","ninth","tenth") tabify(mat,mat_names) Jim On Sun, Jun 13, 2021 at 2:25 AM Jeremie Juste <jeremiejuste at gmail.com> wrote:> > 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 > > ______________________________________________ > 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.