rr-markdownr-packagerglvignette

How to embed an rgl snapshot in Rmarkdown package vignette


I created an R vignette for my package. This vignette embed an interactive rgl figure with webGL.

---
title: "title"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{title}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```r
rgl::plot3d(runif(10), runif(10), runif(10))
```

```{r, echo = FALSE}
rgl::plot3d(runif(10), runif(10), runif(10))
rgl::rglwidget()
```

It works perfectly well but is over-killed for my needs. A simple non interactive picture would be good enough. The problem of the webGL display is that it creates a big file (>1 Mb). Thus I have a NOTE on CRAN about directory size. Rather than arguing about this NOTE I would like to reduce the size of the html output using a regular picture.

rgl::plot3d(runif(10), runif(10), runif(10))
rgl::rgl.snapshot()

This obviously does not work.


Solution

  • You need to use the old hook_rgl method described in the knitr docs, and updated within rgl. For example, these chunks will insert a PNG figure:

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    rgl::setupKnitr()
    ```
    
    ```{r rgl=TRUE, dev='png'}
    rgl::plot3d(runif(10), runif(10), runif(10))
    ```