Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

interactive 3D plots in markdown file - not working anymore?

a couple of months ago, i wanted to embed a 3D interactive plot in a markdown file. looking at previous questions, i found this, which worked great (thanks for that, BTW).

however, when i tried to re do it again today- i can only zoom in and out with the mouse and can not rotate the graph. i've also checked the markdown file that i made before and used to work and i also get the same issue. this happens in Rstudio "browser" thingy (the thing that pops up after you knit) and also in Chrome.

here are two examples, one with rgl package and one with car package, both not working for me (both used to work):

1

```{r setup}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
```
```{r, rgl=TRUE}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
```

2

```{r setup}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
```
```{r testgl, webgl=TRUE}
library(car)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
scatter3d(x, y, z , point.col = "blue", surface=FALSE, xlab = "", ylab = "C", zlab = "")
```

i'm using Rstudio 0.99.489, i've updated all the packages and using R 3.2.3

sessionInfo()
R version 3.2.3 (2015-12-10)
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    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] car_2.1-1     rgl_0.95.1441

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3        lattice_0.20-33    digest_0.6.9       MASS_7.3-45        grid_3.2.3         MatrixModels_0.4-1
 [7] nlme_3.1-122       SparseM_1.7        minqa_1.2.4        nloptr_1.0.4       Matrix_1.2-3       rmarkdown_0.9.2   
[13] splines_3.2.3      lme4_1.1-10        tools_3.2.3        parallel_3.2.3     pbkrtest_0.4-6     yaml_2.1.13       
[19] mgcv_1.8-9         htmltools_0.3      nnet_7.3-11        quantreg_5.19

thank you all for helping

like image 703
isomitzi Avatar asked Feb 03 '16 10:02

isomitzi


1 Answers

After contacting Dr. Yihui Xie (knitr developer) and Professor John Fox (car developer) I think that I can post two options to get around the problem.

Note: These solutions are all the result of the work done by Dr. Xie and Professor Fox and I would like to use this platform to thank them both for taking time to answer my question.

Frst option (no update needed)

Calling next3d() immediately before the call to scatter3d() creates a plot that can be zoomed and rotated in HTML. Consider this simple code as an example:

```{r setup}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
```

```{r, webgl=TRUE}
library(car)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
next3d()
scatter3d(x, y, z, col=rainbow(1000))
```

Second option (package update needed)

Professor Fox has modified the scatter3d() function to call next3d() on its own (instead of rgl.clear()) thus eliminating the need to manually call next3d() before every call to scatter3d(). I have tested it out on my windows machine and can report that it works.

To install the modified package use:

install.packages("car", repos="http://R-Forge.R-project.org") 
like image 165
isomitzi Avatar answered Sep 19 '22 18:09

isomitzi