You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny modal内嵌shinyApp仪表盘无法占满高度 出现多余滚动条

修复方案

核心需要调整3处配置:

  • 补充CSS规则取消modal的垂直滚动限制,同时让内嵌Shiny应用的容器占满父级高度
  • 给plotOutput显式设置匹配modal高度的参数
  • 调整modalDialog的内边距,减少空白浪费

修改后的完整可运行代码如下:

library(shiny)
library(ggplot2)
library(gridExtra)

#main UI
ui <- fluidPage(
  actionButton("show", "Show modal dialog"),
  tags$style(HTML("
    .modal-dialog{ width:1500px}
    .modal-body{ 
      min-height:700px;
      overflow-y: hidden !important; /* 取消垂直滚动条 */
      padding: 0 !important; /* 去掉默认内边距 */
    }
    /* 让内嵌Shiny应用的根容器占满全部高度 */
    .modal-body iframe, .modal-body .shiny-app-container,
    .modal-body html, .modal-body body {
      height: 100% !important;
      margin: 0;
      padding: 0;
    }
  "))
)

#main server
server = function(input, output) {
  observeEvent(input$show, {
    showModal(modalDialog(
      shinyApp(modal_ui, modal_server),
      footer = NULL, # 去掉默认的底部按钮栏,节省空间
      easyClose = TRUE
    ))
  })
}

#UI for the modal
modal_ui <- pageWithSidebar(
  headerPanel('Distribution of Length and Width of Petal and Sepal based on Species'),
  sidebarPanel(
    selectInput('Species', 'Select Species', as.character(unique(iris$Species)))
  ),
  mainPanel(
    # 显式设置绘图高度,匹配modal可用空间
    plotOutput('plot1', height = "620px")
  )
)

#server for the modal
modal_server = function(input, output) {
  data <- reactive({iris[iris$Species == input$Species,]})
  
  output$plot1 <- renderPlot({
    #Plot 1: Distribution of Sepal Length 
    g1 <- ggplot(data(), aes(Sepal.Length))
    g1 <- g1 + geom_histogram(binwidth = .5, fill="green", color = "black",alpha = .2)
    g1 <- g1 + geom_vline( aes(xintercept = mean(data()$Sepal.Length)), colour="red", size=2, alpha=.6)
    g1 <- g1 + labs(x = "Sepal Length")
    g1 <- g1 + labs(y = "Frequency")
    g1 <- g1 + labs(title = paste("Distribution of  Sepal Length, mu =", round(mean(data()$Sepal.Length),2)))
    
    #Plot 2: Disbribution of Sepal Width
    g2 <- ggplot(data(), aes(Sepal.Width))
    g2 <- g2 + geom_histogram(binwidth = .5, fill="green", color = "black",alpha = .2)
    g2 <- g2 + geom_vline( aes(xintercept = mean(data()$Sepal.Width)), colour="red", size=2, alpha=.6)
    g2 <- g2 + labs(x = "Sepal Width")
    g2 <- g2 + labs(y = "Frequency")
    g2 <- g2 + labs(title = paste("Distribution of Sepal Width, mu =", round(mean(data()$Sepal.Width),2)))
    
    #Plot 3: Disbribution of Petal Length
    g3 <- ggplot(data(), aes(Petal.Length))
    g3 <- g3 + geom_histogram(binwidth = .5, fill="yellow", color = "black",alpha = .2)
    g3 <- g3 + geom_vline( aes(xintercept = mean(data()$Petal.Length)), colour="red", size=2, alpha=.6)
    g3 <- g3 + labs(x = "Petal Length")
    g3 <- g3 + labs(y = "Frequency")
    g3 <- g3 + labs(title = paste("Distribution of  Petal Length, mu =", round(mean(data()$Petal.Length),2)))
    
    #Plot 4: Disbribution of Petal Width
    g4 <- ggplot(data(), aes(Petal.Width))
    g4 <- g4 + geom_histogram(binwidth = .5, fill="yellow", color = "black",alpha = .2)
    g4 <- g4 + geom_vline( aes(xintercept = mean(data()$Petal.Width)), colour="red", size=2, alpha=.6)
    g4 <- g4 + labs(x = "Petal Width")
    g4 <- g4 + labs(y = "Frequency")
    g4 <- g4 + labs(title = paste("Distribution of Petal Width, mu =", round(mean(data()$Petal.Width),2)))
    
    #Plotting 4 graphs
    grid.arrange(g1,g2,g3,g4,nrow=2, ncol=2)
  }, height = 620) # renderPlot也同步指定高度,避免缩放变形
}


shinyApp(ui, server)

效果说明

  • 已经移除modal的垂直滚动条,绘图区域会占满modal的全部可用空间
  • 去掉了modal默认的底部按钮栏和内边距,最大化内容显示区域
  • 绘图高度和modal高度完全匹配,不会出现留白或溢出

内容的提问来源于stack exchange,提问作者vDan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:27:01