if (interactive() && !in_pkgdown_example() && requireNamespace("shiny")) {
save <- options(rgl.useNULL = TRUE)
xyz <- matrix(rnorm(300), ncol = 3)
app = shiny::shinyApp(
ui = shiny::bootstrapPage(
shiny::actionButton("redraw", "Redraw"),
rglwidgetOutput("rglPlot")
),
server = function(input, output, session) {
# This waits until the user to click on the "redraw"
# button, then sends a request for the current userMatrix
shiny::observeEvent(input$redraw, {
shinyGetPar3d("userMatrix", session)
})
# This draws the plot whenever input$par3d changes,
# i.e. whenever a response to the request above is
# received.
output$rglPlot <- renderRglwidget({
if (length(rgl.dev.list())) close3d()
col <- sample(colors(), 1)
plot3d(xyz, col = col, type = "s", main = col)
par3d(userMatrix = input$par3d$userMatrix)
rglwidget()
})
})
shiny::runApp(app)
options(save)
}
Run the code above in your browser using DataLab