Saar
Saar

Reputation: 23

r setting the size of a png device to the grob size

I have created a tableGrob T, this tableGrob can be of an altering size, according to previously given parameters.

Is there a way to use the png() function so that the width and height parameters will be taken from the previously created tableGrob

Lets say something like this:

library(gridExtra)

T=tableGrob(mydata)

 png("myfile"
 ,width=convertX(grobWidth(T),"points")    # pseudo-code
 ,height=convertX(grobHeight(T),"points"))
 grid.draw(T)
 dev.off

I get a message that r cannot start the png device and about 50 warnings.

Upvotes: 2

Views: 360

Answers (1)

Hack-R
Hack-R

Reputation: 23200

We can set this up using sum() nested within the convertHeight and convertWidth mehods in the grid package:

grobHeight <- function(x) {
  grid::convertHeight(sum(x$heights), "in", TRUE)
}

grobWidth <- function(x) {
  grid::convertWidth(sum(x$widths), "in", TRUE)
}


png("myfile"
    ,width  = grobWidth(T)
    ,height = grobHeight(T)
      )

Upvotes: 1

Related Questions