user2554330
user2554330

Reputation: 44887

How is shiny::markRenderFunction used?

The question How can I change the size of rgl plots in Shiny RMarkdown? discusses the fact that it's hard to set the size of an rglwidget in a dynamic R Markdown document. The issue is that the Shiny output function sets the size, but there's no obvious way to pass any arguments to it, so the default 512 x 512 size is always used.

The rgl::renderRglwidget function is written according to the example in htmlwidgets::shinyRenderWidget, which provides no way to pass output arguments:

rglwidgetOutput <- function(outputId, width = '512px', height = '512px'){
  shinyWidgetOutput(outputId, 'rglWebGL', width, height, package = 'rgl')
}

renderRglwidget <- function(expr, env = parent.frame(), quoted = FALSE) {
  if (!quoted) { expr <- substitute(expr) } # force quoted
  shinyRenderWidget(expr, rglwidgetOutput, env, quoted = TRUE)
}

It appears from the shiny documentation that it should be using shiny::markRenderFunction to allow for outputArgs to be passed in, but there are no examples that I can find.

What should renderRglwidget look like to make use of shiny::markRenderFunction?

Upvotes: 1

Views: 245

Answers (1)

user2554330
user2554330

Reputation: 44887

Turns out this is fairly simple, though there may be a few caveats. The rglwidgetOutput function was fine as it was, but renderRglwidget needed to be modified to

renderRglwidget <- function(expr, env = parent.frame(), quoted = FALSE, 
                            outputArgs = list()) {
  if (!quoted) { expr <- substitute(expr) } # force quoted
  markRenderFunction(rglwidgetOutput,
                     shinyRenderWidget(expr, rglwidgetOutput, env, quoted = TRUE),
                     outputArgs = outputArgs)
}

The linked question How can I change the size of rgl plots in Shiny RMarkdown? has been edited to show how this is used.

The caveats: setting outputArgs = list(height = "auto") appears to choose zero height rather than the remaining window height. width = "auto" does appear to work after a window resize; before the resize, it looks too big. Similarly, using percentage values for width or height are unsatisfactory. Specifying integer values or strings with px units (e.g. width = "300px") look fine.

Upvotes: 1

Related Questions