SCM

Forum: help

Monitor Forum | Start New Thread Start New Thread
Sprites in Shiny [ Reply ]
By: Yohann Demont on 2020-11-27 16:06
[forum:48498]
Hi,

I am trying to use sprites.

1/ Aspect of the graph
Despite several tries, I don't undestand how to keep aspect of the graph when sprites enter the scene.
Here is a minimal reproducible example

2/ Texture
And another question related to sprites but without example:
Is it possible to directly inject base64 data/uri string as texture rather than using png files ?

Best,
Yohann

# the example for the aspect
require(rgl, quietly = TRUE, warn.conflicts = FALSE)
require(shiny, quietly = TRUE, warn.conflicts = FALSE)

dat = iris[sample(1:nrow(iris), size = 50, replace = FALSE), ]
dat[, 1] = dat[, 1] / 10
dat[, 2] = dat[, 2] * 100
textures = list.files(path = system.file(package = "rgl", "textures"), all.files = FALSE, include.dirs = FALSE, full.names = TRUE)
textures = textures[-1]
textures = sample(textures, size = nrow(dat), replace = TRUE)
xlim = range(dat[, 1])
ylim = range(dat[, 2])
zlim = range(dat[, 3])

ui <- fluidPage(
sidebarLayout(
mainPanel(tags$div(id = "plot_3D_loc",
rglwidgetOutput("plot_3D_1"),
rglwidgetOutput("plot_3D_2"),
rglwidgetOutput("plot_3D_3"),
rglwidgetOutput("plot_3D_4"))),
sidebarPanel(radioButtons(inputId = "set_aspect2", label = "set aspect sprites", choices = c("TRUE", "FALSE"), selected = "TRUE"),
downloadButton(outputId = "save", label = "Save"))
))

server <- function(input, output, session) {
redraw = reactiveValues(observer = numeric())
open3d(useNULL = TRUE)

# here is the aspect I would like to get but with sprites
output$plot_3D_1 <- renderRglwidget({
clear3d()
plot3d(x = dat[, 1:3], type = "s", size = 1, col = as.integer(dat[, "Species"]), aspect = TRUE)
rglwidget()
})

# this one with sprites gets an anormal aspect,
# although images are well positionned (i.e. at their good x,y,z coordinates)
# the bounding box is completly wrong
# and use of aspect(1,1,1) does not solve the problem
output$plot_3D_2 <- renderRglwidget({
clear3d()
# as far as I understood it is not possible to draw several sprites with several different textures
# i.e. texture argument is scalar so I use an lapply do draw every single sprite
lapply(1:length(textures), FUN = function(i_file) {
sprites3d(dat[i_file, ], radius = 10,
col = "grey", fixedSize = FALSE, lit = FALSE,
alpha = 0.6, textype = "rgb",
texture = textures[i_file])
})
decorate3d(xlim = xlim, xlab = "x", ylim = ylim, ylab = "y", zlim = zlim, zlab = "z")
if(input$set_aspect2 == "TRUE") aspect3d(1,1,1)
rglwidget()
})

# if we don't use plot3d but sphere3d and then create the axes/box it is ok
output$plot_3D_3 <- renderRglwidget({
clear3d()
spheres3d(x = dat[, 1:3], radius = 1, col = as.integer(dat[, "Species"]))
aspect3d(1,1,1)
decorate3d(xlim = xlim, xlab = "x", ylim = ylim, ylab = "y", zlim = zlim, zlab = "z")
rglwidget()
})

# if we draw each individual sphere3d it is still ok
output$plot_3D_4 <- renderRglwidget({
clear3d()
lapply(1:nrow(dat), FUN = function(i_row) {
spheres3d(x = dat[i_row, 1:3], radius = 1, col = as.integer(dat[i_row, "Species"]))
})
aspect3d(1,1,1)
decorate3d(xlim = xlim, xlab = "x", ylim = ylim, ylab = "y", zlim = zlim, zlab = "z")
rglwidget()
})
}

shinyApp(ui, server)

Thanks to:
Vienna University of Economics and Business Powered By FusionForge