简体   繁体   English

带有R Markdown的并排rgl图

[英]Side-by-side rgl plots with R Markdown

I am trying to get two 3D scatter plots, drawn with the rgl package, side-by-side in an R Markdown document. 我正在尝试使用rgl包绘制两个3D散点图,并在R Markdown文档中并排绘制。 For example: 例如:

mfrow3d(nr = 1, nc = 2, sharedMouse = TRUE)  
plot3d(mtcars[, 1:3], type = "s")  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()

This works perfectly when run directly in the console but when knit to an HTML document using R Markdown, it seems as if only the second plot is rendered. 这在直接在控制台中运行时非常有效,但是当使用R Markdown编织到HTML文档时,似乎只渲染了第二个图。

I tried adding knit_hooks$set(webgl = hook_webgl) and setting the chunk option webgl = TRUE , but that did not help either. 我尝试添加knit_hooks$set(webgl = hook_webgl)并设置chunk选项webgl = TRUE ,但这也没有帮助。

Any suggestions would be appreciated. 任何建议,将不胜感激。

This is my output from sessionInfo() : 这是sessionInfo()输出:

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] rgl_0.96.0           tidyr_0.6.0          dplyr_0.5.0          smacof_1.8-13       
 [5] emojifont_0.3.3      gridExtra_2.2.1      knitr_1.14           MASS_7.3-45         
 [9] shinydashboard_0.5.1 shiny_0.13.2         RColorBrewer_1.1-2   ggplot2_2.1.0       

loaded via a namespace (and not attached):
 [1] gtools_3.5.0        splines_3.3.1       lattice_0.20-33     colorspace_1.2-6   
 [5] htmltools_0.3.5     yaml_2.1.13         base64enc_0.1-3     chron_2.3-47       
 [9] survival_2.39-4     DBI_0.5             foreign_0.8-66      plyr_1.8.4         
[13] stringr_1.1.0       munsell_0.4.3       gtable_0.2.0        htmlwidgets_0.7    
[17] evaluate_0.9        latticeExtra_0.6-28 httpuv_1.3.3        proto_0.3-10       
[21] Rcpp_0.12.6         acepack_1.3-3.3     xtable_1.8-2        polynom_1.3-8      
[25] scales_0.4.0        formatR_1.4         showtext_0.4-4      gdata_2.17.0       
[29] jsonlite_1.0        Hmisc_3.17-4        sysfonts_0.5        mime_0.5           
[33] weights_0.85        digest_0.6.10       stringi_1.1.1       showtextdb_1.0     
[37] grid_3.3.1          tools_3.3.1         magrittr_1.5        tibble_1.2         
[41] Formula_1.2-1       mice_2.25           cluster_2.0.4       Matrix_1.2-6       
[45] rsconnect_0.4.3     data.table_1.9.6    nnls_1.4            assertthat_0.1     
[49] rmarkdown_1.0.9013  R6_2.1.3            rpart_4.1-10        nnet_7.3-12

This was a bug in the conversion of the bounding box decoration to Javascript code. 这是将边界框装饰转换为Javascript代码的错误。 The bounds from the first plot carried over to the second one. 第一个情节的界限延续到第二个情节。 Since they were calculated from unrelated quantities, you got weird boxes resulting. 由于它们是根据不相关的数量计算出来的,因此产生了奇怪的盒子。 The bug is now fixed in version 0.96.1516, currently only on R-forge. 该错误现已在版本0.96.1516中修复,目前仅在R-forge上。

By the way, with current rgl you don't need hook_webgl or library(rglwidgets) . 顺便说一下,使用当前的rgl你不需要hook_webgllibrary(rglwidgets) Just use library(rgl) and call rglwidget() if you want to insert a plot. 如果要插入绘图,只需使用library(rgl)并调用rglwidget() (There's a way to automatically insert plots without the rglwidget() call, but I don't recommend it.) (有一种方法可以在没有rglwidget()调用的情况下自动插入绘图,但我不推荐它。)

So your document could look like this: 所以你的文件看起来像这样:

```{r echo=TRUE}
library(rgl)
mfrow3d(nr = 1, nc = 2, sharedMouse = TRUE)  
plot3d(mtcars[, 1:3], type = "s",)  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()
```

This produces the picture below in rgl 0.96.1516: 这将生成下面的图片, rgl 0.96.1516:

在此输入图像描述

There seems to be at least one, and more like two bugs in play here. 似乎至少有一个,更像是在这里玩两个错误。 Modifying your code a bit, this: 修改你的代码,这个:

```{r echo=T}
library(rgl)
library(rglwidget)
library(knitr)
knit_hooks$set(webgl = hook_webgl)

mfrow3d(nr = 2, nc = 2, sharedMouse = T)  
plot3d(mtcars[, 1:3], type = "s",)  
plot3d(mtcars[, 4:6], type = "s")
plot3d(mtcars[, 1:3], type = "s")  
plot3d(mtcars[, 4:6], type = "s")
rglwidget()
```

produces this in a briefly visible device window: 在一个简短可见的设备窗口中生成它:

在此输入图像描述

but this in the R-Studio preview window: 但这在R-Studio预览窗口中:

在此输入图像描述

And this in the html file loaded into the Chrome browser: 这在加载到Chrome浏览器的html文件中:

在此输入图像描述

So only the first one works, and markdown seems to break it. 所以只有第一个有效,降价似乎打破了它。

I would say you should probably just avoid composing multiple rgl plots in markdown until this (these) bugs get fixed. 我会说你应该避免在markdown中组合多个rgl图,直到这些(这些)错误得到修复。 Might be hard as it seems to be some weird interaction between the various graphics devices and the library. 可能很难,因为它似乎是各种图形设备和库之间的一些奇怪的交互。

Update 更新

Looks like D (?) fixed the issue per his answer, but there seems to be another issue in play too. 看起来D(?)根据他的答案解决了问题,但似乎还有其他问题在起作用。 Posting this as information. 将此信息作为信息发布。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM