Created
May 25, 2012 22:26
-
-
Save yihui/2790922 to your computer and use it in GitHub Desktop.
draw a table by gridExtra in knitr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{article} | |
\begin{document} | |
We can draw a table with the gridExtra package, and one problem is to | |
pre-determine the size of the table plot to remove the extra white | |
margin. Below is an example that shows you how to do it (courtesy of | |
baptiste auguie). | |
<<setup>>= | |
library(gridExtra) | |
g = tableGrob(head(iris, 4)) | |
<<draw-table, dev='png', dpi=150, fig.width=convertWidth(grobWidth(g), "in", value=TRUE), fig.height=convertHeight(grobHeight(g), "in", value=TRUE)>>= | |
grid.draw(g) | |
@ | |
\end{document} |
I see. I did not know convertWidth()
has to open a device. I'll fix this. Thanks!
fixed; will appear in my next commit
Thanks!
Was looking for exactly this! Is there any way to reduce the amount of typing involved? I'm using your code in a similar way with Rmd:
```{r}
g = tableGrob(head(iris, 4))
```{r draw-table, dev='png', dpi=72, fig.width=convertWidth(grobWidth(g), "in", value=TRUE), fig.height=convertHeight(grobHeight(g), "in", value=TRUE)}
grid.draw(g)
```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, I didn't know that. Regarding
Rplots.pdf
, it is definitely created when I knit the gist above. Do you mean that it's not knitr's fault that it exists (which is correct, grid opens a device also in interactive use to test the fontsize)?