9

I'm looking at implementing 3D interactive plots in my shiny App, and so far I've been using plotly. However, plotly has one major disadvantage, it's extremely slow when rendering. I've done checks, and the whole creation of updated outplot$plot <- renderPlotly ({.....}) and plotlyOutput("plot") takes less than 0.5 seconds, despite the large data set involved. This is a know issue for years but still seems to be current.

Hence, I'm looking to use a package called car, also because it has many options, some which I particularly want that are not available in other packages. Info on the car package is here: http://www.sthda.com/english/wiki/amazing-interactive-3d-scatter-plots-r-software-and-data-visualization The problem is that it renders in a separate popup window rather than inside the shiny app, and I want to have it inside it, or even better, add a button that allow the user to have it as a popup, but only when asked. However, i can't figure out how to jam the bugger into the actual shiny page.

Here is my minimal example with a single text element and the graph code that (in my case) keeps appearing in a separate window rather than in the app.

install.packages(c("rgl", "car", "shiny"))

library("rgl")
library("car")
library(shiny)

cars$time <- cars$dist/cars$speed


ui <- fluidPage(
  hr("how do we get the plot inside this app window rather than in a popup?"),

  plotOutput("plot",  width = 800, height = 600)
  )


server <- (function(input, output) {


  output$plot <- renderPlot({
    scatter3d(x=cars$speed, y=cars$dist, z=cars$time, surface=FALSE, ellipsoid = TRUE)
    })
})


shinyApp(ui = ui, server = server)

There is also this package, scatterplot3d but that's not interactive http://www.sthda.com/english/wiki/scatterplot3d-3d-graphics-r-software-and-data-visualization

And there are some RGL packages but they have the same issue (seperate window) and don't offer the options I am lookign for.

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
Mark
  • 2,789
  • 1
  • 26
  • 66

1 Answers1

12

You need to use an rglwidget which takes the last rgl plot and puts in in an htmlwidget. It used to be in a separate package, but it has recently been integrated into `rgl.

Here is the code to do this:

library(rgl)
library(car)
library(shiny)

cars$time <- cars$dist/cars$speed

ui <- fluidPage(
  hr("how do we get the plot inside this app window rather than in a popup?"),

  rglwidgetOutput("plot",  width = 800, height = 600)
)

server <- (function(input, output) {

  output$plot <- renderRglwidget({
    rgl.open(useNULL=T)
    scatter3d(x=cars$speed, y=cars$dist, z=cars$time, surface=FALSE, ellipsoid = TRUE)
    rglwidget()
  })
})   
shinyApp(ui = ui, server = server)

Yielding this:

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Hey Mike, thanks! I only found a webGL wrapper for rgl which ended up printing copyright info rather than a plot haha. This works, however, now I get a popup AND embedded plot. Did it do this for you too? If not. How do I make my popup stop. Thanks ! – Mark May 22 '17 at 10:32
  • Okay, fixed that too. Try it out. If it works an upvote would be apprecated :) – Mike Wise May 22 '17 at 10:44
  • Actually I am still looking at it. Think it can be a lot simpler. – Mike Wise May 22 '17 at 10:53
  • Hm, works to get rid of the popup (I tried the rgl.close trick too), but now it's not rotatable anymore. – Mark May 22 '17 at 10:59
  • Weird, it is actually the left mouse buttondown that is not being recognized, because you can still zoom in and out with the mouse center button. Possibly some kind of bug. – Mike Wise May 22 '17 at 11:01
  • I would post that as a new question but this time use the tag 'rgl'. That will get the rgl authors attention and he will know what is going on. I will watch it too. – Mike Wise May 22 '17 at 11:03
  • ok, posted the new question. was there a variety of your trial code where you actually had a popup and could spin the actual graph inside shiny? Seems I can't spin it in either case, with rgl.open(useNULL=F) or rgl.open(useNULL=T) – Mark May 22 '17 at 11:48
  • I thought there was, but I couldn't repro when I tried just now. Something is very weird, it even affects programs I run afterwards. Probably a new bug. – Mike Wise May 22 '17 at 12:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144835/discussion-between-mark-and-mike-wise). – Mark May 22 '17 at 12:20
  • I think it was. Look at this post: https://stackoverflow.com/questions/44112083/rgl-in-r-shiny-not-rotating-on-left-mouse-button-click – Mike Wise Jul 11 '17 at 17:26