Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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)

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.

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.

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.

Note

R and the browser don't maintain a perfect match between the way parameters are stored internally. The browser version of parameters will be returned by shinyGetPar3d and should be supplied to shinySetPar3d.

Author(s)

Duncan Murdoch

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

if (interactive() && !in_pkgdown_example()) {
  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)
}

rgl

3D Visualization Using OpenGL

v0.106.8
GPL
Authors
Duncan Murdoch [aut, cre], Daniel Adler [aut], Oleg Nenadic [ctb], Simon Urbanek [ctb], Ming Chen [ctb], Albrecht Gebhardt [ctb], Ben Bolker [ctb], Gabor Csardi [ctb], Adam Strzelecki [ctb], Alexander Senger [ctb], The R Core Team [ctb, cph], Dirk Eddelbuettel [ctb], The authors of Shiny [cph], The authors of knitr [cph], Jeroen Ooms [ctb], Yohann Demont [ctb], Joshua Ulrich [ctb], Xavier Fernandez i Marin [ctb], George Helffrich [ctb], Ivan Krylov [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.