我正在循环中使用plot3d {rgl}绘制一个三维图形。 knitr rgl hooks适用于单个3d图形,但在循环中使用时,markdown文件不包含3d图形。
答案 0 :(得分:3)
你可以做的一种方法是使用mfrow3d()
来放置多个" subscenes"在单个"场景"中,然后使用rglwidget()
命令在html中显示场景。查看更多信息here。以下是来自Yihui's answer的脚本的大量修改版本:
---
output: html_document
---
You will need to install the rglwidget library if you have not already.
```{r setup}
library(knitr)
library(rgl)
library(rglwidget)
```
Using mfrow3d() I create a scene with 5 subscenes,
so each of the 5 plots will appear in a single scene.
Each will still be independently interactive (can zoom in on one at a time, e.g.).
```{r testgl}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
mfrow3d(nr = 5, nc = 1)
cols <- c("red", "orange", "green", "blue", "purple")
for(i in 1:5){
plot3d(x, y, z, col = cols[i])
}
rglwidget(height = 4000, width = 1000)
```
答案 1 :(得分:2)
另一种解决方案是包含多个rglwidget()
值。您需要将它们全部置于htmltools::tagList()
的调用中才能使其正常工作。
(如果您使用https://r-forge.r-project.org/R/?group_id=234中rglwidget
的开发版本,则不会需要rgl
包。修改文斯的例子,
---
output: html_document
---
```{r setup}
library(rgl)
library(rglwidget)
```
```{r testgl}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
cols <- c("red", "orange", "green", "blue", "purple")
result <- list()
for(i in 1:5){
plot3d(x, y, z, col = cols[i])
result[[i]] <- rglwidget()
}
htmltools::tagList(result)
```