调整 Shiny RMarkdown 报告中的大图尺寸

Adjust for large figure size in Shiny RMarkdown report

我正在开发一个闪亮的 RMarkdown 报告,其中包含一个部分,允许用户通过根据不同变量(例如,主题、课程、作业)对数据集进行分组来创建不同的脊线图。然而,一些变量只有几个组(例如主题),而其他变量有很多组(例如分配)。对于具有许多组的变量,生成的图形变得不可读,因此我想增加图形大小或允许用户以某种方式向下滚动图形。有没有人对我如何做到这一点有任何建议? (下面带有虚拟数据的 Rmd 文件示例)

---
title: "Test"
author: "R User"
date: "9/7/2021"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)

# example data
df <- data.frame(
  subject = c(rep("A", 1000), rep("B", 1000), rep("C", 1000)),
  course = rep(paste0("Course ", as.character(1:300)), 10),
  value = rnorm(3000)
)
```

## Modify figure size

I would like to modify the figure size so the ridgelines are still readable when grouped by course, either by making the figure size larger overall or allowing the user to scroll down the figure.

```{r, echo=FALSE}
inputPanel(
  selectInput("group", label = "Group",
              choices = c("subject", "course"))
)

renderPlot({
  ggplot(df, aes(y = !!as.symbol(input$group), x = value)) +
    ggridges::geom_density_ridges(color = "grey95", fill = "grey50", alpha = 0.5) +
    geom_boxplot(fill = "grey95", color = "grey40", width = 0.2, outlier.shape = NA) +
    labs(y = "") +
    theme_minimal()
})
```

我们可以添加一个滑动条让用户选择情节的高度,这样当他们觉得太挤的时候,他们可以增加高度。

---
title: "Test"
author: "R User"
date: "9/7/2021"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(shiny)
# example data
df <- data.frame(
  subject = c(rep("A", 1000), rep("B", 1000), rep("C", 1000)),
  course = rep(paste0("Course ", as.character(1:300)), 10),
  value = rnorm(3000)
)
```

## Modify figure size

I would like to modify the figure size so the ridgelines are still readable when grouped by course, either by making the figure size larger overall or allowing the user to scroll down the figure.

```{r, echo=FALSE}
inputPanel(
  selectInput("group", label = "Group",
              choices = c("subject", "course")),
  sliderInput("p_height", "Plot height", value = 600, min = 400, max = 1000, step = 50, round = TRUE)
)

renderPlot({
  ggplot(df, aes(y = !!as.symbol(input$group), x = value)) +
    ggridges::geom_density_ridges(color = "grey95", fill = "grey50", alpha = 0.5) +
    geom_boxplot(fill = "grey95", color = "grey40", width = 0.2, outlier.shape = NA) +
    labs(y = "") +
    theme_minimal()
}, height = exprToFunction(input$p_height))
```

把默认的valueminmax改成你想要的数字,单位是像素。

我从来没有跟进我想出的答案...这是一种不需要为将来遇到相同问题的其他人添加滑块的方法,(1) 使用我的示例和 (2)使用带有 built-in 个数据集的更小版本:

版本 1

---
title: "Test"
author: "R User"
date: "9/7/2021"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(dplyr)
# example data
df <- data.frame(
  subject = c(rep("A", 1000), rep("B", 1000), rep("C", 1000)),
  course = rep(paste0("Course ", as.character(1:300)), 10),
  value = rnorm(3000)
)
```

## Modify figure size

I would like to modify the figure size so the ridgelines are still readable when grouped by course, either by making the figure size larger overall or allowing the user to scroll down the figure.

```{r, echo=FALSE}
inputPanel(
  selectInput("group", label = "Group",
              choices = c("subject", "course"))
)

plot_height <- reactive({
  df %>%
    pull(get(input$group)) %>%
    unique() %>%
    length()
})

renderPlot({
  ggplot(df, aes(y = !!as.symbol(input$group), x = value)) +
    geom_boxplot() +
    theme_minimal()
}, height = function() { 50 * plot_height() })
```

版本 2

---
title: "Dynamic Plot Height"
author: "Natalie O'Shea"
date: '2022-05-31'
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(dplyr)
library(ggplot2)
```

```{r}
inputPanel(
  selectInput("grouping", label = "Select grouping:", 
              choices = c("supp", "dose"))
)

plot_height <- reactive({
  ToothGrowth %>%
    pull(get(input$grouping)) %>%
    unique() %>%
    length()
})

renderPlot({
  ToothGrowth %>%
    ggplot(aes(y = len)) +
    geom_histogram() +
    facet_wrap(~get(input$grouping), ncol = 1)
}, height = function() { 100 * plot_height() })
```