[R] sweave tables as images?

Yihui Xie xie at yihui.name
Sat May 26 00:32:04 CEST 2012


Normally I hate the device()+code+dev.off()+cat('\\includegraphics{})
trick. Let me show you how trivial this can be in knitr:
https://gist.github.com/2790922

This example shows the real source code to the reader
(device()+dev.off() makes no sense to them), and leaves all the dirty
tricks behind the scene.

That being said, Sweave can be regarded as a subset of knitr, so if
you really want to play the old trick, you still can do it in knitr.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Fri, May 25, 2012 at 5:01 PM, baptiste auguie
<baptiste.auguie at googlemail.com> wrote:
> My guess is that it would be impossible to use the table size in
> Sweave, only mildly non-trivial in knitr with a proper hook. The
> easiest hack in either of them would be to manually open the png
> device, and use cat() with \includegraphics{} where you want the plot
> diplayed.
>
> HTH,
>
> b.
>
> On 26 May 2012 09:43, Alexander Shenkin <ashenkin at ufl.edu> wrote:
>> this works - thanks baptiste!  i'm working in Sweave right now - perhaps
>> it will be tough in knitr as you mention.
>>
>> On 5/25/2012 4:31 PM, baptiste auguie wrote:
>>> you can open a device that has the exact dimensions of the table,
>>>
>>> g = tableGrob(head(iris, 4))
>>> png("test.png", width=convertWidth(grobWidth(g), "in", value=TRUE),
>>>         height=convertHeight(grobHeight(g), "in",
>>> value=TRUE),units="in", res=150)
>>> grid.draw(g)
>>> dev.off()
>>>
>>> Doing this with knitr might be tricky though, since the unit
>>> conversion opens a blank device window, and you'd want to define some
>>> hook instead of manually creating the png file.
>>>
>>> I have another version of grid.table where you can specify the width
>>> and height manually [*], e.g to span the full window, but it's not
>>> necessarily a desirable thing (the spacing between rows and columns
>>> can become too large).
>>>
>>>
>>> HTH,
>>>
>>> baptiste
>>>
>>>
>>> [*] experimental code at https://gist.github.com/2013903
>>>
>>> On 26 May 2012 09:16, Alexander Shenkin <ashenkin at ufl.edu> wrote:
>>>> Thanks Yihui,
>>>>
>>>> That's a great idea, and comes close to the mark, except that I have to
>>>> use png's in order to "Insert & Link" them as pictures in Word (and
>>>> hence make the doc both shareable and update when new figures are
>>>> generated).
>>>>
>>>> thanks,
>>>> allie
>>>>
>>>> On 5/25/2012 2:57 PM, Yihui Xie wrote:
>>>>> You may take a look at knitr's graphics manual which tells you how you
>>>>> can automatically crop the white margins:
>>>>> https://github.com/downloads/yihui/knitr/knitr-graphics.pdf ("Cropping
>>>>> PDF Graphics").
>>>>>
>>>>> I'm not sure if pdfcrop works in this case, though.
>>>>>
>>>>> Regards,
>>>>> Yihui
>>>>> --
>>>>> Yihui Xie <xieyihui at gmail.com>
>>>>> Phone: 515-294-2465 Web: http://yihui.name
>>>>> Department of Statistics, Iowa State University
>>>>> 2215 Snedecor Hall, Ames, IA
>>>>>
>>>>>
>>>>> On Fri, May 25, 2012 at 2:44 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
>>>>>> grid.table() works well, but using it in sweave creates graphics with
>>>>>> very wide margins.  I'm sure this has something to do with grid, and not
>>>>>> just grid.table.  Any idea how I can clip the graphic to the edges of
>>>>>> the table graphic?  I've looked into viewports, etc, but I can't seem to
>>>>>> find anything that will clip a graphic to its edges, perhaps with some
>>>>>> defined margin.
>>>>>>
>>>>>> any help greatly appreciated!
>>>>>>
>>>>>> thanks,
>>>>>> allie
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list