rgl icon indicating copy to clipboard operation
rgl copied to clipboard

Code and output messed up in R Markdown after rgl plot

Open dmurdoch opened this issue 2 years ago • 1 comments

If the chunk option has collapse = TRUE and any htmlwidget (including one from rgl) is displayed there, then code and output following the widget will not be formatted properly.

Problematically, the rmarkdown::html_vignette template defaults to collapse = TRUE.

This is a knitr issue, fixed in https://github.com/yihui/knitr/pull/2212. That fix is included in knitr version 1.42.3, currently only on Github. You can install it with

remotes::install_github("yihui/knitr")

dmurdoch avatar Jan 26 '23 20:01 dmurdoch

A workaround if you want collapse = TRUE is to end any chunk at the point where it shows an rgl (or other htmlwidget) display, and start a new chunk afterwards. For example, change this:

```{r collapse = TRUE}
xyz <- matrix(rnorm(30), ncol = 3)
plot3d(xyz)
plot3d(xyz, type = "s")
```

to this:

```{r collapse = TRUE}
xyz <- matrix(rnorm(30), ncol = 3)
plot3d(xyz)
```
```{r collapse = TRUE}
plot3d(xyz, type = "s")
```

Another workaround is to use chunk option fig.show = "hold", which will display all the rgl figures after the chunk, instead of showing them as they are completed. For example,

```{r collapse = TRUE, fig.show = "hold"}
xyz <- matrix(rnorm(30), ncol = 3)
plot3d(xyz)
plot3d(xyz, type = "s")
```

dmurdoch avatar Jan 26 '23 20:01 dmurdoch