How can I change the size of rgl plots in Shiny RMarkdown?

戏子无情 提交于 2019-12-23 21:41:15

问题


I'm creating a Shiny document using RMarkdown and am including reactive plots from rgl. I cannot, however, change the viewport size of the rendered plots. That is, I cannot change the height and width of the space in which the plots render. How might I make the rendered plots bigger than their default?

EDIT: Alternating fig.width in the knitr options does not change the width of Shiny render functions, it seems.

Below is the document I have so far.

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

回答1:


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 chunk 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 make it into rgl, and you won't need to use myRenderRglwidget, you'll just be able to use renderRglwidget with the extra argument.



来源:https://stackoverflow.com/questions/43009133/how-can-i-change-the-size-of-rgl-plots-in-shiny-rmarkdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!