[R] printing an arbitrary-length character vector in columns on a page of a pdf report

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Wed Jun 6 05:10:53 CEST 2018


I think this is cuter, and it is a hair faster.

n <- length(dd)
ddm <- matrix("", (n+2) %/% nc, nc)
ddm[1:n] <- dd

Rich


> system.time(for (i in 1:10000) {
+ add <- nc - (length(dd) %% nc)
+ dd2 <- c(dd, rep("", add))
+ ddm <- matrix(dd2, ncol = nc)
+ })
   user  system elapsed
  0.064   0.100   0.483
>
> system.time(for (i in 1:10000) {
+ n <- length(dd)
+ ddm <- matrix("", (n+2) %/% nc, nc)
+ ddm[1:n] <- dd
+ })
   user  system elapsed
  0.045   0.000   0.045

On Tue, Jun 5, 2018 at 12:45 PM, Christopher W Ryan
<cryan using binghamton.edu> wrote:
> I'm writing code for a recurring report, using an R --> Sweave --> pdflatex
> workflow. It includes a character vector of short words that I would like
> to display compactly, in columns on a page, rather than one word per line,
> which would waste a lot of space. The vector of words will increase
> unpredictably over time, with future versions of the report.
>
> I thought I would go about it by turning the character vector into a
> matrix, as follows:
>
> dd <- LETTERS
> ## set number of columns. Three for now
> nc <- 3
> ## have to pad the character vector to a length that is multiple of nc
> add <- nc - (length(dd) %% nc)
> dd2 <- c(dd, rep("", add))
> ddm <- matrix(dd2, ncol = nc)
> library(Hmisc)
> latex(ddm, file = "")
>
> Any ideas for a more elegant way to do this?
>
> Thanks.
>
> --Chris Ryan
> Binghamton University
> and
> Broome County Health Department
> Binghamton, NY, US
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.




More information about the R-help mailing list