'How to use rayshader / rgl in shiny?

I'm trying to use these nice plots from the rayshader package in my shiny app but all I receive is a black window where the plot should be.

server:

output$ray <- renderPlot({

ggdiamonds = ggplot(df_surf, aes(Inj_D, Inj_L)) +
stat_density_2d(aes(fill = stat(nlevel))
                , geom = "polygon"
                , n = 200
                , bins = 50
                ,contour = TRUE) +
facet_wrap(Tube~.) +
scale_fill_viridis_c(option = "A") + theme_bw()

options("cores"=2)
plot_gg(ggdiamonds, multicore = TRUE)
render_camera(zoom=0.5,theta=-30,phi=30)
# render_snapshot(clear = FALSE)
# rgl::rglwidget()
})

ui:

mainPanel(
           plotOutput("ray", height = 700)
)

edit: Ok, I got it working by simply adding rglwidget() to the server part.. I also changed renderPlot() to renderRglwidget() and plotOutput() to rglwidgetOutput(). Additionally, I put options(rgl.useNULL = TRUE) into the first line of server.R and global.R. I'll leave this post so someone else might stumble over this.

In case it's too confusing:

ui:

 mainPanel(
    rglwidgetOutput("ray", height = 700)
  )

server:

 output$ray <- renderRglwidget({
    try(rgl.close())
  
      # size, shape, facets..?
    ggdiamonds = ggplot(df_surf, aes(Inj_D, Inj_L)) +
    stat_density_2d(aes(fill = stat(nlevel))
                    , geom = "polygon"
                    , n = 200
                    , bins = 50
                    ,contour = TRUE) +
    facet_wrap(Tube~.) +
    scale_fill_viridis_c(option = "A") + theme_bw()
    
    options("cores"=2)
    plot_gg(ggdiamonds, multicore = TRUE)
    rglwidget()
  })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source