2

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.

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Saar
  • 23
  • 4

1 Answers1

1

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)
      )
Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • trying this I'm getting 'font family not found warnings – Saar Aug 14 '18 at 08:44
  • @Saar I can't replicate that, but I don't think it sounds related. If you think I'm wrong we can troubleshoot the reproducibility in SO chat – Hack-R Aug 15 '18 at 01:44
  • Hi @hack-R 7 , I'm rather new to R and therefore am not able to tell exactly whether it is related or not. I will gladly put a larger piece of my code wherever you want (BTW what is SO chat?) so we'll be able to keep troubleshooting. – Saar Aug 15 '18 at 09:46
  • @saar SO is short for Stack Overflow. They have a chat we can use for extended discussions (I should give you a link... I'll get back to you on that) – Hack-R Aug 15 '18 at 13:42
  • I have not had the time to chase that in the last two weeks, and it seems that so have you, I'm getting the follwing warnings: In grid.Call(C_textBounds, as.graphicsAnnot(x$label), ... : font family not found in Windows font database. It happens both on a pdf and a png device. – Saar Aug 28 '18 at 08:37
  • Update: works beautifully on pdf, but not in png. Warnings are still there in both graphic devices. – Saar Aug 28 '18 at 09:57
  • gsave("test.tiff", width = grobWidth(tab1), height = grobHeight(tab1)) Worked for me, thank you! – twb10 Aug 17 '19 at 22:19