调整 Shiny RMarkdown 报告中的大图大小

Posted

技术标签:

【中文标题】调整 Shiny RMarkdown 报告中的大图大小【英文标题】:Adjust for large figure size in Shiny RMarkdown report 【发布时间】:2021-11-04 11:46:06 【问题描述】:

我正在开发一个 Shiny 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()
)
```

【问题讨论】:

【参考方案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(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更改为您想要的数字,单位为像素。

【讨论】:

以上是关于调整 Shiny RMarkdown 报告中的大图大小的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有 Shiny 的情况下过滤 Rmarkdown 中的预聚合数据?

即使在示例页面中,Shiny-server 也不显示 rmarkdown(已安装 rmarkdown 包)

改变图形大小(Rstudio、Rmarkdown、Shiny)

使用 Shiny 链接到 RMarkdown 上的本地 html 文件

如何在 RMarkdown 中使用 Shiny 进行嵌套选择 selectInput()?

将多个情节图下载到 PDF Shiny