5

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

Community
  • 1
  • 1
isomitzi
  • 131
  • 9

3 Answers3

5

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") 
CL.
  • 14,577
  • 5
  • 46
  • 73
isomitzi
  • 131
  • 9
2

try using threejs

It works without problems and IMO looks better

```{r, echo=FALSE}
library(threejs)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
scatterplot3js(x, y, z,  color=rainbow(1000), renderer="canvas")
```
  • thank you for helping! it does work but i need the additional features in the "car" package - regression surface, clustering , etc' i've looked at the package but, as of now, can't seem to figure out if this package provide this. – isomitzi Feb 04 '16 at 17:44
1

First of all try installing the rglwidget package install.packages('rglwidget')

Next, your example 1 seems to work when a space is added between code chunks

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

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

I'm unsure as to why the example two is no longer working though

aeongrail
  • 1,304
  • 1
  • 13
  • 26
  • thank you for helping but installing rglwidget doesn't seem to help. are you using windows? – isomitzi Feb 05 '16 at 10:06
  • Yes, I find it interesting that if I remove the newline between the code chunks this non longer works on my machine though – aeongrail Feb 08 '16 at 18:55
  • 1
    Generally I'd suggest using the `rgl::setupKnitr` function rather than calling `knit_hooks()` yourself. The interface between `rgl` and `knitr` has been changing quite a bit recently; `setupKnitr` should keep up with the changes. – user2554330 Feb 21 '16 at 21:50