How do I resize rgl plots in Shiny RMarkdown?

I am creating a brilliant document using RMarkdown and include reactive plots from rgl

. However, I cannot resize the viewport of the displayed graphs. That is, I cannot change the height and width of the space in which the graphs are displayed. How can I make the displayed charts larger than their default?

EDIT: Interleaving fig.width

in knitr parameters does not change the width of the Shiny render functions. It seems.

Below is the document I have.

---
title: "Untitled"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rgl)
library(dplyr)
source("https://raw.githubusercontent.com/trestletech/rgl/master/vignettes/setup.R")
```

```{r, rgl = TRUE, echo = FALSE}
inputPanel(
  sliderInput("b1", label = withMathJax("\\(\\beta_1\\)"), min = -.4, max = .4, value = .4, step = .025),
  sliderInput("b2", label = withMathJax("\\(\\beta_2\\)"), min = -.4, max = .4, value = .4, step = .025),
  sliderInput("b3", label = withMathJax("\\(\\beta_3\\)"), min = -.2, max = .2, value = .2, step = .025)
)

r <- reactive({
  set.seed(12479)
  nobs <- 200
  x <- rnorm(nobs)
  z <- rnorm(nobs)
  r2 <- input$b1^2 + input$b2^2 + input$b3^2
  y <- input$b1 * x + input$b2 * z + input$b3 * x*z + rnorm(nobs, sd = sqrt(1 - r2))
  d <- data.frame(x, z, y)

  fit_int <-   lm(y ~ x*z, data = d)
  fit_noint <- lm(y ~ x+z, data = d)

  scale <- seq(-3, 3, length.out = 30)

  dp <- expand.grid(x = scale, z = scale, KEEP.OUT.ATTRS = FALSE)

  dp_int <- dp %>%
    mutate(., y = predict(fit_int, .)) %>%
    filter(-3 <= y, y <= 3)

  dp_noint <- dp %>%
    mutate(., y = predict(fit_noint, .)) %>%
    filter(-3 <= y, y <= 3)

  return(list(
    d = d, 
    fit_int = fit_int, 
    fit_noint = fit_noint,
    dp_int = dp_int,
    dp_noint = dp_noint))
})

renderRglwidget({
  try(rgl.close())
  layout3d(matrix(c(1,1,2,3), byrow = TRUE, ncol = 2), sharedMouse = TRUE)

  with(r()$d, plot3d(x, z, y, xlim = c(-3, 3), ylim = c(-3, 3), zlim = c(-3, 3), main = "scatter"))

  next3d()
  with(r()$d, plot3d(x, z, y, xlim = c(-3, 3), ylim = c(-3, 3), zlim = c(-3, 3), main = "with int"))
  with(r()$dp_int, points3d(
    x, z, y, col = rgb(1,0,0), alpha = .25))

  next3d()
  with(r()$d, plot3d(x, z, y, xlim = c(-3, 3), ylim = c(-3, 3), zlim = c(-3, 3), main = "without int"))
  with(r()$dp_noint, points3d(
    x, z, y, col = rgb(0,.5,0), alpha = .25))

  rglwidget(width = 1400)
})
```

      

+3


source to share


1 answer


Add this function to your code:

myRenderRglwidget <- 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)
}

      

Then, instead of calling, renderRglwidget

call myRenderRglwidget

and add an extra argument at the end outputArgs = list(width = 1400)

, i.e. the last few lines of your code snippet should be



  with(r()$dp_noint, points3d(
    x, z, y, col = rgb(0,.5,0), alpha = .25))

  rglwidget()
}, outputArgs = list(width = 1400))

      

This modification will eventually turn into rgl

, and you won't need to use myRenderRglwidget

, you can just use renderRglwidget

with an additional argument.

+1


source







All Articles