You need to enable JavaScript to run this app. Shiny如何隐藏侧边栏selectInput控件并为绘图指定固定临床参数
优惠活动
大模型
产品
解决方案
定价
更多

Shiny如何隐藏侧边栏selectInput控件并为绘图指定固定临床参数

实现方案

你为每个临床参数设置的selectInput都仅绑定单个固定可选值,本身不具备实际交互选择作用,只需要移除侧边栏的冗余控件,把服务端对这些input变量的引用替换为对应固定常量,就能完全匹配原有功能,且不会在界面渲染多余控件。

具体修改操作

  • 精简侧边栏组件:仅保留MicroRNA的选择控件,删除所有和p1到p5相关的临床参数选择控件代码
  • 替换服务端引用:把绘图代码里原来引用input$p1、input$p2这类input变量的位置,直接替换为每个绘图对应的固定临床列名字符串,和原有selectInput返回的默认值完全等价

修改后完整可运行代码

data_prep <- structure(list(miRNA = c("hsa-let-7a-3p", "hsa-let-7a-3p", "hsa-let-7a-3p"
), ID = c("86", "175", "217"), value = c(5.57979757386892, 5.21619202802748, 
5.42796072966512), Serum_M_component = c("IgG", "IgG", "Unknown"
), ISS_stage = c("Stage 3", "Stage 1", "Stage 3"), del17 = c("Poor Sample", 
"No", "No"), t4_14 = c("Poor Sample", "No", "Yes"), Bone_disease = c("No", 
"Yes", "Yes")), row.names = c(NA, 3L), class = "data.frame")

ui.miRNA.clinical <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("MicroRNA expression <br/> in Multiple myeloma"))),
  # 侧边栏仅保留MicroRNA选择控件,删除所有冗余临床参数选择器
  dashboardSidebar(
    selectInput("gene", "MicroRNA", choices = unique(data_prep$miRNA))
  ),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Plot Bone_disease", plotOutput("myplot1", width = "400px", height = "300px")),
      tabPanel("Plot Serum_M_component", plotOutput("myplot2", width = "400px", height = "300px")),
      tabPanel("Plot ISS_stage", plotOutput("myplot3", width = "400px", height = "300px")),
      tabPanel("Plot del17", plotOutput("myplot4", width = "400px", height = "300px")),
      tabPanel("Plot t4_14", plotOutput("myplot5", width = "400px", height = "300px"))
      
    )
  )
)

server.miRNA.clinical <- function(input, output, session) {
  
  # filter data by Gene
  data_selected <- reactive({
    filter(data_prep, miRNA %in% input$gene)
  })
  
  # 绘图时直接使用固定临床列名,替代原input变量引用
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes_string("Bone_disease", "value", fill = "Bone_disease")) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "wilcox.test")
  })
  output$myplot2 <- renderPlot({
    ggplot(data_selected(), aes_string("Serum_M_component", "value", fill = "Serum_M_component")) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
  })
  
  output$myplot3 <- renderPlot({
    ggplot(data_selected(), aes_string("ISS_stage", "value", fill = "ISS_stage")) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
  })
  
  output$myplot4 <- renderPlot({
    ggplot(data_selected(), aes_string("del17", "value", fill = "del17")) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
  })
  
  output$myplot5 <- renderPlot({
    ggplot(data_selected(), aes_string("t4_14", "value", fill = "t4_14")) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
  })
}

shinyApp(ui.miRNA.clinical, server.miRNA.clinical)

这类和标签页固定绑定的临床参数不存在用户交互调整需求,硬编码固定值的方式完全不会改变原有绘图逻辑,同时还能减少不必要的响应式依赖,降低应用的冗余计算开销。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.02 22:15:03