multiple rgl chunks - close device?

65 views
Skip to first unread message

baptiste auguie

unread,
Nov 19, 2012, 4:01:28 AM11/19/12
to knitr
Dear list,

I'm attempting to write some documentation featuring rgl plots with knitr markdown. Problem: the plot snapshot from the second chunk features both the first and second plot scenes, suggesting that the device wasn't closed after the first chunk. How do I do this?

Thanks,

baptiste

## example


```{r load,message=FALSE}
library(rgl)
knit_hooks$set(rgl = hook_rgl)
```

```{r ch1, rgl=TRUE}
rgl.spheres(0,0,0)                      
```

```{r ch2, rgl=TRUE}
rgl.spheres(0,2,0)                      
```

Yihui Xie

unread,
Nov 19, 2012, 12:51:14 PM11/19/12
to baptiste auguie, knitr
I think you can tweak the rgl hook a little bit, e.g.

knit_hooks$set(rgl = function(before, options, envir) {
# if a device was opened before this chunk, close it
if (before && rgl.cur() > 0) rgl.close()
hook_rgl(before, options, envir)
})

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

baptiste auguie

unread,
Nov 19, 2012, 1:43:50 PM11/19/12
to Yihui Xie, knitr
Thanks Yihui, that works!

Best,
baptiste
Reply all
Reply to author
Forward
0 new messages