Flexdashboard中Shiny滑块显示过小,请求修复为正常尺寸
解决flexdashboard中Shiny滑块显示迷你尺寸的问题
问题原因
你在flexdashboard的.sidebar列中嵌套了Shiny原生的sidebarPanel()组件,这会引发样式冲突——flexdashboard的侧边栏容器本身已自带布局样式,sidebarPanel()的内边距和容器规则会把滑块挤压成迷你尺寸。
修复方案
直接移除sidebarPanel(),将sliderInput()直接放在侧边栏的代码块中即可恢复正常样式。同时清理代码中重复的图表定义和渲染逻辑,避免冗余:
修复后的完整代码:
--- title: "Test App" output: flexdashboard::flex_dashboard: orientation: columns vertical_layout: fill runtime: shiny --- ```{r setup, include=FALSE} library(flexdashboard) library(shiny) library(tidyverse) library(shinyjs) library(shinyWidgets) # Data Set 1 df <- data.frame( cyl = c("4","6","8"), Multiplier = c(2,4,6) ) # Data Set 2 df1 <- mtcars df1$cyl <- as.factor(df1$cyl)
Column {.sidebar}
useShinyjs(rmd = TRUE) selectInput("clusterNum", label = h4("Charts"), choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"), selected = "Chart1" ) # 移除sidebarPanel,直接放置sliderInput sliderInput("integer", "Integer:", min = 0, max = 8, value = 1)
Column {data-width=650}
Chart
# First chart Chart1 <- ggplot(df1, aes(x = wt, y = mpg)) + geom_point() # 合并重复的Chart2逻辑 Chart2 <- reactive({ dplyr::left_join(df, df1, by = "cyl") %>% dplyr::mutate(mpg_new = mpg * Multiplier * input$integer) %>% ggplot(aes(x = wt, y = mpg_new)) + geom_point() }) # 只保留一份renderPlot renderPlot({ switch(input$clusterNum, "Chart1" = Chart1, "Chart2" = Chart2() ) })
### 额外说明 - flexdashboard的侧边栏通过`Column {.sidebar}`定义,无需再使用Shiny的`sidebarPanel()`,两者布局系统不兼容会导致样式异常。 - 原代码中重复定义了`Chart2`和两次`renderPlot`,清理后可避免逻辑冗余,提升运行效率。 内容的提问来源于stack exchange,提问作者silent_hunter
相关产品推荐
相关产品推荐

