问题描述
我正在使用 RMarkdown 创建一个 Shiny 文档,并包含来自 rgl
的反应图.但是,我无法更改渲染图的视口大小.也就是说,我无法更改绘图所在空间的高度和宽度.如何使渲染图大于默认值?
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?
在 knitr 选项中交替 fig.width
似乎不会改变 Shiny 渲染函数的宽度.
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)
})
```
推荐答案
将此函数添加到您的代码中:
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)
}
然后不是调用 renderRglwidget
,而是调用 myRenderRglwidget
,然后在末尾添加一个额外的参数,outputArgs = list(width = 1400)
,即代码块的最后几行应该是
Then instead of calling renderRglwidget
, call myRenderRglwidget
, andadd 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))
这个修改最终会变成rgl
,你不需要使用myRenderRglwidget
,你只需要使用renderRglwidget
带有额外的参数.
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.
这篇关于如何在 Shiny RMarkdown 中更改 rgl 图的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!