yihui / knitr

A general-purpose tool for dynamic report generation in R
https://yihui.org/knitr/
2.39k stars 878 forks source link

Completed example with rgl animation #952

Closed rain1024 closed 9 years ago

rain1024 commented 9 years ago

I've had hard 2 days to find a completed example rgl animation with knitr.

Can you provide an completed example, such as animation for toss a 3d coin in rmarkdown?

Thank you so much.

This below code is not working


---
  output:
  html_document:
  keep_md: yes

---

  ```{r setup, results='asis'}
library(knitr)
knit_hooks$set(webgl = hook_webgl)
knit_hooks$set(rgl.static = hook_rgl) # use to capture a single rgl graphic
knit_hooks$set(rgl.mov = hook_plot_custom) # use to capture a movie
library(rgl)
library(animation)
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
open3d()
Sys.sleep(2)
spheres3d(x, y, z, col=rainbow(1000))
rain1024 commented 9 years ago

I make a progress toward,

this below code worked :dancer:

---
  output:
    html_document:
      keep_md: yes
---

```{r setup, results='asis'}
library(knitr)
knit_hooks$set(webgl = hook_webgl)
knit_hooks$set(rgl.static = hook_rgl) # use to capture a single rgl graphic
knit_hooks$set(rgl.mov = hook_plot_custom) # use to capture a movie
library(rgl)
library(animation)

for(i in 1:20){
  x <- seq(0 + (1 * 0.05), 3 + (1 * 0.05), length= 20)
  y <- x
  f <- function(x, y) { sin(x * y) }
  z <- outer(x, y, f)
  persp(x, y, z, theta = 45, phi = i, expand = 0.4, col = "orange")
}


I discover a new thing, `knitr` will capture `persp` function and product animation move.

But I can find a way to make animation using `open3d` and `plot3d`
yihui commented 9 years ago

Please see the graphics manual: http://yihui.name/knitr/demo/graphics/ You need to explicitly save rgl graphics.

github-actions[bot] commented 4 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.