how do you display all data points in a table, auto size the table width and height

StackOverflow https://stackoverflow.com/questions/17998281

  •  04-06-2022
  •  | 
  •  

Question

I have a data frame that I would like to create a table and save it as a png file.

I am doing this:

library(gridExtra)
my_table<- tableGrob(y[,1:3],gpar.coretext =gpar(fontsize=8),gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8))
png("cpu.png")
grid.arrange(my_table)
dev.off()

I need this png file to include all the data in my table. if my table is big, it is cut off. How would I make sure that all the data is displayed on this table?

Était-ce utile?

La solution

Your png file currently has the default height; you can specify it explicitly to make sure the whole table fits on the page.

library(gridExtra)
d <- head(iris, 50)
tb <- tableGrob(d)

h <- convertHeight(grobHeight(tb), "in", valueOnly=TRUE)
w <- convertWidth(grobWidth(tb), "in", valueOnly=TRUE)

png("iris.png", width = w, height = h, units = "in", res=300)
grid.draw(tb)
dev.off()

enter image description here

It's not perfectly accurate, for some reason, but it should help.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top