Learn R Programming

rgl (version 1.3.1)

shinyGetPar3d: Communicate RGL parameters between R and Javascript in Shiny

Description

These functions allow Shiny apps to read and write the par3d settings that may have been modified by user interaction in the browser.

Usage

shinyGetPar3d(parameters, session, subscene = currentSubscene3d(cur3d()), tag = "")
shinySetPar3d(..., session, subscene = currentSubscene3d(cur3d()))
shinyResetBrush(session, brush)

Value

These functions are called for their side effects, and don't return useful values.

The side effect of shinyGetPar3d is to cause input$par3d

to be updated sometime later. Besides the requested parameter values, input$par3d will contain a copy of the subscene and tag arguments.

The side effect of shinySetPar3d is to send a message to the browser to update its copy of the par3d parameters immediately.

Arguments

parameters

A character vector naming the parameters to get.

session

The Shiny session object.

subscene

The subscene to which the parameters apply. Defaults to the currently active subscene in the R session.

tag

An arbitrary string or value which will be sent as part of the response.

...

A number of name = value pairs to be modified, or a single named list of parameters. Entries named tag or subscene will be ignored.

brush

The name of a Shiny input element corresponding to the shinyBrush argument to rglwidget.

Author

Duncan Murdoch

Details

Requesting information from the browser is a complicated process. The shinyGetPar3d function doesn't return the requested value, it just submits a request for the value to be returned later in input$par3d, a reactive input. No action will result except when a reactive observer depends on input$par3d. See the example code below.

The shinySetPar3d function sends a message to the browser asking it to change a particular parameter. The change will be made immediately, without sending the full scene to the browser, so should be reasonably fast.

References

https://shiny.rstudio.com/articles/communicating-with-js.html describes the underlying mechanisms used by these two functions.

See Also

The rglwidget argument shinySelectionInput allows information about mouse selections to be returned to R.

Examples

Run this code
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