在R中的WebGL playwidget中打印3D组动画

在R中的WebGL playwidget中打印3D组动画,r,rgl,R,Rgl,我正在使用R WebGL创建一个3D交互模型,以绘制数据组。下面的脚本用于打印所有数据组,每组都有单独的颜色。input.txt中包含的数据只是整个数据的一小部分。重要的是这些工具必须是交互式的(鼠标控制缩放和旋转),因此为什么使用plot3D而不是其他工具 library(rgl) library(plot3D) d <- ncol(read.table("./input.txt", header = F)) colClasses <- replace(rep

我正在使用R WebGL创建一个3D交互模型,以绘制数据组。下面的脚本用于打印所有数据组,每组都有单独的颜色。input.txt中包含的数据只是整个数据的一小部分。重要的是这些工具必须是交互式的(鼠标控制缩放和旋转),因此为什么使用plot3D而不是其他工具

library(rgl)
library(plot3D)

d <- ncol(read.table("./input.txt", header = F))
colClasses <- replace(rep("NULL", d), c(1,2,3,4), NA)
d.data <- lapply("./input.txt", read.table, header = F, colClasses = colClasses)
dval <- do.call("rbind", d.data)

df <- data.frame(dval)
colnames(df) <- c("id", "z", "y", "x")

summary(df)

plot3d(x=df$x, y=df$y, z=df$z, col = df$id, type = 's', radius = 2,
  xlab="X", ylab="Y", zlab="Z")

writeWebGL( filename="input.html" ,  width=600, height=600)
我想扩展它来绘制完整的数据,而不是一小部分。这将包括将每一帧作为每组的交互式3D绘图,就像上面脚本的输出一样,而是让动画只显示每一帧的一组,并通过播放按钮在所有组中播放。显示了我希望在“控件>子控件”下执行的操作的示例。显示他们如何操作的代码:

rglwidget() %>%
playwidget(start = 0, stop = 3, interval = 1,
       subsetControl(1, subsets = list(
                 Setosa = setosa,
                 Versicolor = versicolor,
                 Virginica = virginica,
                 All = c(setosa, versicolor, virginica)
                 )))
我不希望最终图像显示所有组,也不能使用类似于上面示例的
列表
,因为组的数量太多。我试图在下面的脚本中包含它们的示例,但没有效果,只是在没有按钮控件的情况下同时显示3D绘图中的所有组(与原始脚本的结果相同)

库(rgl)
图书馆(plot3D)

d我认为该代码符合您的要求:

# First, plot everything, and save the ids of the objects in the plot
plotids <- with(df,
   plot3d(x=x, y=y, z=z, col = id, type = 's', radius = 2,
          xlab="X", ylab="Y", zlab="Z"))

# Now plot the different ids one at a time:
ids <- list()
for (i in unique(df$id)) 
  ids <- c(ids, list(with(subset(df, id == i), spheres3d(x,y,z,col=id,radius = 2))))

# Add the full dataset to the list of ids
ids <- c(ids, all = plotids["data"])

# Now plot the scene and add the playwidget.
rglwidget() %>%
  playwidget(start = 0, stop = length(ids) - 1, interval = 1,
             subsetControl(1, subsets = ids,
             ))
#首先,绘制所有内容,并在绘图中保存对象的ID

plotids我认为这段代码符合您的要求:

# First, plot everything, and save the ids of the objects in the plot
plotids <- with(df,
   plot3d(x=x, y=y, z=z, col = id, type = 's', radius = 2,
          xlab="X", ylab="Y", zlab="Z"))

# Now plot the different ids one at a time:
ids <- list()
for (i in unique(df$id)) 
  ids <- c(ids, list(with(subset(df, id == i), spheres3d(x,y,z,col=id,radius = 2))))

# Add the full dataset to the list of ids
ids <- c(ids, all = plotids["data"])

# Now plot the scene and add the playwidget.
rglwidget() %>%
  playwidget(start = 0, stop = length(ids) - 1, interval = 1,
             subsetControl(1, subsets = ids,
             ))
#首先,绘制所有内容,并在绘图中保存对象的ID

plotId为什么不能为
子集
参数使用列表?它只是类似于
as.list(df$id)
。顺便说一下,不要使用
writeWebGL
。它已弃用。您的代码未运行。它假设输入中有8列。我已编辑脚本以使用4列,这样它将适合您。将
c(1,2,7,8)
更改为
c(1,2,3,4)
,这是一个简单的修复方法
writeWebGL
一直在工作,但是还有什么我应该使用的吗?为什么不能为
子集
参数使用列表?它只是类似于
as.list(df$id)
。顺便说一下,不要使用
writeWebGL
。它已弃用。您的代码未运行。它假设输入中有8列。我已编辑脚本以使用4列,这样它将适合您。将
c(1,2,7,8)
更改为
c(1,2,3,4)
,这是一个简单的修复方法
writeWebGL
一直在工作,但是还有什么我应该使用的吗?我不熟悉R降价。我将此添加到您的代码中:
widget%
,在所包含脚本的底部,
f它没有为我显示这一点。您可能没有执行与我发布的代码相同的代码。我忘了我有
id,我不熟悉R Markdown。我将此添加到您的代码中:
widget%
,在所包含脚本的底部,
f它没有为我显示这一点。您可能没有执行与我发布的代码相同的代码。我忘了我有
id
# First, plot everything, and save the ids of the objects in the plot
plotids <- with(df,
   plot3d(x=x, y=y, z=z, col = id, type = 's', radius = 2,
          xlab="X", ylab="Y", zlab="Z"))

# Now plot the different ids one at a time:
ids <- list()
for (i in unique(df$id)) 
  ids <- c(ids, list(with(subset(df, id == i), spheres3d(x,y,z,col=id,radius = 2))))

# Add the full dataset to the list of ids
ids <- c(ids, all = plotids["data"])

# Now plot the scene and add the playwidget.
rglwidget() %>%
  playwidget(start = 0, stop = length(ids) - 1, interval = 1,
             subsetControl(1, subsets = ids,
             ))