R: rgl link 多个图到一个小部件

R: rgl link multiple plots to a single widget

我正在尝试 link 一个 playwidget() 滑块到多个绘图,以便滑块影响所有绘图。我想在 Rmarkdown 文件中使用它,而不是在 Shiny 应用程序中使用它。

我设法在 subsetControl 中添加了绘图并添加了 subscenes 控件,但它无法正常工作:第一个子集工作正常,但如果我移动滑块,我会得到第一个绘图(黑色和红色点)在两个图中重复。

library(rgl)

open3d() # Remove the earlier display

layout3d(matrix(c(1,2), nrow=1), sharedMouse = T)

next3d()
setosa <- with(subset(iris, Species == "setosa"), 
               spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                         col="black",
                         radius = 0.211))
versicolor <- with(subset(iris, Species == "versicolor"), 
                   spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                             col="red",
                             radius = 0.211))

next3d()
setosa2 <- with(subset(iris, Species == "setosa"), 
                spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                          col="yellow",
                          radius = 0.211))
versicolor2 <- with(subset(iris, Species == "versicolor"), 
                    spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                              col="blue",
                              radius = 0.211))


rglwidget() %>%
  playwidget(start = 0, stop = 2, interval = 1,
             subsetControl(1, subscenes = subsceneList(), subsets = list(
               All = c(setosa, setosa2, versicolor, versicolor2),
               Setosa = c(setosa, setosa2),
               Versicolor = c(versicolor, versicolor2)
             )))

rgl个子场景使用的模型是根拥有所有对象,每个子场景显示其中的一部分。您的代码首先在第一个子场景中显示 setosaversicolor,在第二个子场景中显示 setosa2versicolor2,但是子集控件表示显示 setosasetosa2 在一个子集中,both versicolorversicolor2 在另一个子集中,然后做这在两个 子场景中。由于 setosasetosa2 具有相同的形状和位置,因此一次只出现一个:绘制的第一个。

要得到你想要的东西,你需要两个 subsetControl,它们都由同一个 playwidget 控制,例如

library(rgl)

open3d() # Remove the earlier display

layout3d(matrix(c(1,2), nrow=1), sharedMouse = T)

next3d()
sub1 <- subsceneInfo()$id
setosa <- with(subset(iris, Species == "setosa"), 
                             spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                                                col="black",
                                                radius = 0.211))
versicolor <- with(subset(iris, Species == "versicolor"), 
                                     spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                                                        col="red",
                                                        radius = 0.211))

next3d()
sub2 <- subsceneInfo()$id
setosa2 <- with(subset(iris, Species == "setosa"), 
                                spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                                                    col="yellow",
                                                    radius = 0.211))
versicolor2 <- with(subset(iris, Species == "versicolor"), 
                                        spheres3d(Sepal.Length, Sepal.Width, Petal.Length, 
                                                            col="blue",
                                                            radius = 0.211))


rglwidget() %>%
    playwidget(start = 0, stop = 2, interval = 1,
                         list(subsetControl(1, subscenes = sub1, subsets = list(
                            All = c(setosa, versicolor),
                            Setosa = setosa,
                            Versicolor = versicolor
                         )),
                         subsetControl(1, subscenes = sub2, subsets = list(
                            All = c(setosa2, versicolor2),
                            Setosa = setosa2,
                            Versicolor = versicolor2
                         ))))