Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rglwidget: Cannot replace previous 3d plot

Tags:

r

3d

shiny

rgl

The example coming with package rglwidget is fairly complex; I created a simple shiny/rglwidget example below. The plot appears ok, but each time I toggle the checkbox, a new image is appended in the browser window.

How can I replace/overwrite the first plot?

options(rgl.useNULL = TRUE)
library(shiny)
library(rglwidget)
library(rgl)

app = shinyApp(
  ui = bootstrapPage(
      checkboxInput("rescale", "Rescale"),
      rglwidgetOutput("rglPlot")
  ),
  server = function(input, output) {
    output$rglPlot <- renderRglwidget({
      try(rgl.close()) # added following @user2554330
      if (input$rescale) aspect3d(1,1,10) else aspect3d(1,1,1)
      # High level function following @user2554330: same effect
      # plot3d(rnorm(100), rnorm(100), rnorm(100,sd = 0.1), 
      #     col = rainbow(1000))
      spheres3d(rnorm(100), rnorm(100), rnorm(100,sd = 0.1), col = "red",
                radius = 0.1)
      axes3d()
      rglwidget()

    })
  })
runApp(app)

Session Info:

R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rgl_0.95.1429      rglwidget_0.1.1431 shiny_0.12.2      

loaded via a namespace (and not attached):
 [1] htmlwidgets_0.5.1 R6_2.1.1          rsconnect_0.4.1.9 htmltools_0.2.7  
 [5] tools_3.2.2       yaml_2.1.13       Rcpp_0.12.2       knitr_1.11       
 [9] jsonlite_0.9.19   digest_0.6.8      xtable_1.8-0      Cairo_1.5-9      
[13] httpuv_1.3.3      mime_0.4     
like image 417
Dieter Menne Avatar asked Oct 30 '22 13:10

Dieter Menne


1 Answers

It acts just like rgl -- when you use low-level commands (spheres3d, axes3d) they are added to the existing plot. If you want to replace it, the simplest way is to call rgl.close() first. next3d() is another possibility, though it's slightly different.

Alternatively, call high level functions like plot3d or persp3d. They clear the window before drawing.

Edited to add: What I described above is what is supposed to happen. I see the duplicate outputs; they're a bug. I've fixed it in version 0.1.1433 on R-forge.

like image 151
user2554330 Avatar answered Nov 15 '22 07:11

user2554330