如何为RShiny仪表板盒子设置垂直滚动以显示全尺寸条形图
解决Shiny中水平条形图压缩与滚动问题
针对你遇到的水平条形图在Shiny盒子内被压缩、右对齐,且无法滚动显示的问题,可通过以下调整实现图表全尺寸显示+盒子垂直滚动:
关键修改点
- 给容器盒子添加固定高度和垂直滚动样式
- 为plotly图表设置适配分类数量的高度,关闭自动缩放
- 过滤数据中空分类值,优化图表显示
修改后的完整代码
library(shiny) library(shinydashboard) library(tidyverse) library(plotly) ae <- structure(list(aept = c("Upper respiratory tract infection", "Fungal infection", "COVID-19", "COVID-19", "Anxiety", "Weight increased", "Hyperlipidaemia", "Hyperglycaemia", "Influenza", "Neck pain", "Electrocardiogram QT prolonged", "Irritability", "Headache", "Contusion", "Headache", "Headache", "Headache", "Anaemia", "Candida infection", "Headache", "Headache", "Headache", "Upper respiratory tract infection", "Diarrhoea", "COVID-19", "Influenza", "Electrocardiogram T wave abnormal", "Headache", "Headache", "Headache", "Diarrhoea", "Upper respiratory tract infection", "Headache", "Vaginal infection", "Tonsillitis bacterial", "Peritonsillar abscess", "Atrioventricular block first degree", "Procedural dizziness", "Influenza", "Procedural dizziness", "Viral infection", "Diarrhoea", "Nausea", "Rhinitis", "Diarrhoea", "Upper respiratory tract infection", "Diarrhoea", "Upper respiratory tract infection", "Headache", "Rhinitis", "Upper respiratory tract infection", "Gamma-glutamyltransferase increased", "Gamma-glutamyltransferase increased", "Headache", "Headache", "Headache", "Insomnia", "", "", "Headache", "Glomerular filtration rate decreased", "Blood creatinine increased", "Headache", "Ear pain", "Headache", "Headache", "Rhinitis allergic", "Oropharyngeal pain", "Headache", "Diarrhoea", "Headache", "Headache", "Headache", "Urinary tract infection", "Headache", "Vaginal discharge", "Anaemia", "Influenza", "Bronchitis", "Upper respiratory tract infection", "Rhinitis", "Blood thyroid stimulating hormone decreased", "Abdominal pain upper", "Headache", "Tonsillitis", "Headache", "Headache", "Methaemoglobinaemia", "Abdominal pain", "Abdominal pain lower", "Blood creatine phosphokinase increased", "Electrocardiogram QT prolonged", "Headache", "Headache", "Electrocardiogram QT prolonged", "Electrocardiogram QT prolonged", "Headache", "Headache", "Upper respiratory tract infection", "Upper respiratory tract infection")), row.names = c(NA, 100L ), class = "data.frame") server <- function(input, output){ #AE Preferred Term output$ae_pt <- renderPlotly({ # 过滤空分类值,优化图表 ae_clean <- ae %>% filter(aept != "") p <- ae_clean %>% group_by(aept) %>% summarise(count=n()) %>% arrange(desc(count)) %>% ggplot(aes(x=reorder(aept,count), y=count)) + geom_bar(stat='identity') + coord_flip() + theme_minimal() # 根据分类数量设置图表高度,关闭自动缩放 ggplotly(p) %>% layout(height = length(unique(ae_clean$aept)) * 30, autosize = FALSE) }) } ui <- dashboardPage( dashboardHeader(), dashboardSidebar( sidebarMenu( menuItem("Adverse Events", tabName = "ae", icon = icon("heart")) )), dashboardBody( tabItems( # Adverse Events Tab ---- tabItem(tabName="ae", # 设置盒子高度并开启垂直滚动 fluidRow(box(plotlyOutput("ae_pt"), title="Preferred Term", style = "height: 600px; overflow-y: auto;")) ) ))) shinyApp(ui, server)
代码解释
- 容器样式调整:在
box中添加style = "height: 600px; overflow-y: auto;",固定盒子高度为600px,当图表内容超出时自动显示垂直滚动条。 - 图表高度适配:通过
length(unique(ae_clean$aept)) * 30计算图表高度,每个分类分配30px高度,确保所有条形都能完整显示;autosize = FALSE关闭自动缩放,避免图表被压缩。 - 数据清理:过滤掉空字符串的
aept值,避免图表中出现无意义的空条形。
内容的提问来源于stack exchange,提问作者RJ Irv
相关产品推荐
相关产品推荐

