4

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

Michael
  • 154
  • 1
  • 11

1 Answers1

8

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")
```     
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Thanks, @user2554330. That is brilliant. Also, your reference to `knitr` made me realise that I had been reading in the wrong places: Details on the graphics engine turn out to be documented in the [knitr graphics manual](https://yihui.name/knitr/), and not in the `R Markdown` documentation, as I had presumed. – Michael Aug 07 '19 at 14:19