R: dynamic text annotation in rglwidget playwidget()

Viewed 35

I want to create a rgl widget with a slider using playwidget() including a different text annotation for each subset.

If I run the following

clear3d()

a1 <- spheres3d(xyz.coords(matrix(sample.int(100, 81), ncol = 3)), col="blue", radius = 2)
text3d(10, 10, 10, "text1")
a2 <- spheres3d(xyz.coords(matrix(sample.int(100, 27), ncol = 3)), col="red", radius = 2)
text3d(30, 5, 5, "text2")
a3 <- spheres3d(xyz.coords(matrix(sample.int(100, 9), ncol = 3)), col="green", radius = 2)
text3d(5, 5, 50, "text3")

rglwidget() %>%
  playwidget(start = 0, stop = 2, interval = 1,
             components = c("Reverse", "Play", "Slower", "Faster",
                            "Reset", "Slider", "Label"),
             labels = c("a1", "a2", "a3"),
             subsetControl(1, subsets = list(aa=a1,bb=a2,cc=a3)))

I get the 3 text annotations fixed for all subsets. Any idea on how to fix this?

A more general question is how to bind additional rglLowlevel objects (such as box3d()) to each single subset so to be changing when moving the slider bar. Thanks

1 Answers

The results from both spheres3d and text3d are rgl ids, which are simply integer values with a class. You can just use c() to join them into vectors. Modify your example to this and it works:

library(rgl)
clear3d()

a1 <- c(spheres3d(xyz.coords(matrix(sample.int(100, 81), ncol = 3)), col="blue", radius = 2),
        text3d(10, 10, 10, "text1"))
a2 <- c(spheres3d(xyz.coords(matrix(sample.int(100, 27), ncol = 3)), col="red", radius = 2),
        text3d(30, 5, 5, "text2"))
a3 <- c(spheres3d(xyz.coords(matrix(sample.int(100, 9), ncol = 3)), col="green", radius = 2), 
        text3d(5, 5, 50, "text3"))

rglwidget() %>%
  playwidget(start = 0, stop = 2, interval = 1,
             components = c("Reverse", "Play", "Slower", "Faster",
                            "Reset", "Slider", "Label"),
             labels = c("a1", "a2", "a3"),
             subsetControl(1, subsets = list(aa=a1,bb=a2,cc=a3)))
Related