Search code examples
r-markdownbeamertikzdevice

use of tikzdevice in RMarkdown with beamer output


I was wondering whether tikzdevice package can be used for producing R plots in RMarkdown, to be exported to a beamer presentation.

I would have thought that the solution I am looking for might be similar to the example with Sweave on page 15 of the tikzdevice manual.

What I have tried, without knowing exactly what I am doing, is to amend Yihui's example from the RMarkdown book:

---
title: "Habits"
author: John Doe
date: March 22, 2005
output: beamer_presentation
---

# In the morning

## Getting up

- Turn off alarm
- Get out of bed

---

```{r, echo=FALSE, results='tex'}
require(tikzDevice)
tikz(console=TRUE)
plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
dummy <- dev.off()
```     

Unfortunatly, this does not result in the tikz plot being rendered in the beamer presentation. Does anyone see how this can be achieved? Thanks.

Regards, Michael


Solution

  • There are a few problems in what you wrote. LaTeX needs to be told to use the tikz package; and in knitr, you use results='asis' instead of results='tex'. Finally, you would need to tell tikz() what size figure to use.

    However, rather than fix those things, it's easier to tell knitr to handle everything, by using the 'tikz' graphics device. So this works:

    ---
    title:  Demo with dev='tikz'
    output: beamer_presentation
    ---
    
    ## A Tikz plot
    
    ```{r echo=FALSE, dev='tikz'}
    plot(sin, -pi, 2*pi, main="A Stand Alone TikZ Plot")
    ```