[R] most stable way to output text layout

Jeremie Juste jerem|eju@te @end|ng |rom gm@||@com
Sat Jun 12 23:49:56 CEST 2021


Hello,

Many thanks to you all for the prompt reply. Thanks for sharing.

On Saturday, 12 Jun 2021 at 09:53, Jeff Newmiller wrote:
> 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")
>       })

@Jeff your solution seems to suit my need the most
Just reducing the field width to 8 and added a minus sign for left justification.

     apply(
      mat,1,
      function(x) {
        x[is.na(x)] <-""
        cat(paste(sprintf("-%8s",x)),"\n")
      })

On Saturday, 12 Jun 2021 at 20:04, Greg Minshall wrote:
> 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")
>     })
@Greg many thanks for your input, the mx variable will certainly come in handy.


On Saturday, 12 Jun 2021 at 14:00, Avi Gross via R-help wrote:
> mat <- sprintf("%-*s", col_width, mat)

@Avi many thanks for the left justification. It did encourage me to read
the sprintf help more carefully.


With the solution above, I get a razor thin front-end. To map columns
name interactively with barely 200 lines of code, and I can trigger the
debugger at anytime to inspect the full table. Very powerful.

Best regards,
Jeremie



More information about the R-help mailing list