3

I am using R studio and R markdown (with knitr) to create pdf or html report of my results. I have then used function kable to make the table look nice on a PDF output or function htmlTable of library expss for html output. I have created a big table for example like:

cbind(row.number=1:10,iris[1:10,],iris[1:10,],iris[1:10,]) -> tbl1

The problem is that this table has too many columns they go outside the width of the paper. Is there a way to wrap the table with one column repeated as first column. Using the table in the example above, the output should be something like:

row.number Sepal.Length Sepal.Width Petal.Length 1 1 5.1 3.5 1.4 2 2 4.9 3.0 1.4 3 3 4.7 3.2 1.3

row.number Petal.Width Species Sepal.Length 1 1 0.2 setosa 5.1 2 2 0.2 setosa 4.9 3 3 0.2 setosa 4.7

row.number Sepal.Width Petal.Length Petal.Width 1 1 3.5 1.4 0.2 2 2 3.0 1.4 0.2 3 3 3.2 1.3 0.2

To be continued like this. I created this just by hand, I have not solved the problem already :)

I saw here that it is possible to decrease the size to the table, but that is not rely what I was looking for. Automatically adjust LaTeX table width to fit pdf using knitr and Rstudio

Gauti
  • 83
  • 6
  • Possible duplicate of [Longtable in a knitr (PDF) document: using xtable (or kable)](https://stackoverflow.com/questions/32265676/longtable-in-a-knitr-pdf-document-using-xtable-or-kable) – Yehuda Oct 22 '18 at 13:12
  • https://stackoverflow.com/questions/32265676/longtable-in-a-knitr-pdf-document-using-xtable-or-kable is similar but not exactly the same. – Gauti Oct 23 '18 at 09:54
  • How about rotating the table by 90 degrees? Or switching to landscape format for that page? – Ralf Stubner Oct 23 '18 at 10:14
  • I tried rotating, but that was not exactly the look I was looking for. I ended up I ended up copying the data to Excel and played around with the table there. I will leave this open to see if I get more ideas. – Gauti Oct 25 '18 at 08:46

1 Answers1

0

I feel I may be missing something obvious here but I don't see why you can't set the rownames on the table to be your row.number column and then just create a loop creating a table for batches of 3 columns.

Doesn't

cbind(iris[1:10,],iris[1:10,],iris[1:10,]) -> tbl1
rownames(tbl1) <- 1:10
kable(tbl1[,1:3])
kable(tbl1[,4:7])
kable(tbl1[,8:10])

do what you want?

  • I ended up doing something similar to what you suggest here. I was hoping there was a way to wrap this automatically. – Gauti Feb 22 '21 at 12:39