【问题标题】:Function to define variable to plot定义要绘制的变量的函数
【发布时间】:2021-05-22 10:03:42
【问题描述】:

我想通过定义为output$myplot1output$myplot2 等的几个绘图函数制作多个绘图,这些绘图当前在参数selectInput 中定义,例如p1 是 Bone_disease。但是,我想使用 selectInput 之外的另一个函数来定义要为 myplot1 和 myplot2 绘制哪些参数,因为我不希望它显示在侧边栏中。

如何将此行替换为定义要绘制的临床参数的类似行,但不在侧边栏中显示:selectInput("p1", "Clinical parameter", choices = c("Serum_M_component"))

我想在侧边栏中显示的唯一参数是MicroRNA

!更新了示例输入数据。

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"))),
  dashboardSidebar(
    selectInput("gene", "MicroRNA", choices = unique(data_prep$miRNA)),
    selectInput("p1", "Clinical parameter", choices = c("Bone_disease"),),
    selectInput("p2", "Clinical parameter", choices = c("Serum_M_component")),
    selectInput("p3", "Clinical parameter", choices = c("ISS_stage")),
    selectInput("p4", "Clinical parameter", choices = c("del17")),
    selectInput("p5", "Clinical parameter", choices = c("t4_14"))),
  
  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)
  })
  
  # Plot. use aes_string to simply use character input p
  #my_comparisons <- list( c("Yes", "No"), c("Stage 1", "Stage 3"))
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p1, "value", fill = input$p1)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
      stat_compare_means(method = "wilcox.test")
      #method ="anova"
  })
  output$myplot2 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p2, "value", fill = input$p2)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot3 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p3, "value", fill = input$p3)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot4 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p4, "value", fill = input$p4)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
  
  output$myplot5 <- renderPlot({
    ggplot(data_selected(), aes_string(input$p5, "value", fill = input$p5)) + 
      geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
    stat_compare_means(method = "anova") 
    #method ="anova"
  })
}

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

【问题讨论】:

    标签: r shiny shinydashboard shiny-server


    【解决方案1】:

    一种方法是使用shinyjs 包的hidden 选项。试试这个

                                                                                                                                                                               "Yes", "Yes")), row.names = c(NA, 3L), class = "data.frame")
    library(shinyjs)
    
    ui.miRNA.clinical <- dashboardPage(
      # Application title
      dashboardHeader(title=h4(HTML("MicroRNA expression <br/> in Multiple myeloma"))),
      dashboardSidebar(
        useShinyjs(),
        selectInput("gene", "MicroRNA", choices = unique(data_prep$miRNA)),
        shinyjs::hidden(
          selectInput("p1", "Clinical parameter", choices = c("Bone_disease"),),
          selectInput("p2", "Clinical parameter", choices = c("Serum_M_component")),
          selectInput("p3", "Clinical parameter", choices = c("ISS_stage")),
          selectInput("p4", "Clinical parameter", choices = c("del17")),
          selectInput("p5", "Clinical parameter", choices = c("t4_14"))
        )
        
        ),
      
      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)
      })
      
      # Plot. use aes_string to simply use character input p
      #my_comparisons <- list( c("Yes", "No"), c("Stage 1", "Stage 3"))
      output$myplot1 <- renderPlot({
        ggplot(data_selected(), aes_string(input$p1, "value", fill = input$p1)) + 
          geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
          stat_compare_means(method = "wilcox.test")
        #method ="anova"
      })
      output$myplot2 <- renderPlot({
        ggplot(data_selected(), aes_string(input$p2, "value", fill = input$p2)) + 
          geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
          stat_compare_means(method = "anova") 
        #method ="anova"
      })
      
      output$myplot3 <- renderPlot({
        ggplot(data_selected(), aes_string(input$p3, "value", fill = input$p3)) + 
          geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
          stat_compare_means(method = "anova") 
        #method ="anova"
      })
      
      output$myplot4 <- renderPlot({
        ggplot(data_selected(), aes_string(input$p4, "value", fill = input$p4)) + 
          geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
          stat_compare_means(method = "anova") 
        #method ="anova"
      })
      
      output$myplot5 <- renderPlot({
        ggplot(data_selected(), aes_string(input$p5, "value", fill = input$p5)) + 
          geom_boxplot() + theme_classic(base_size = 12) + labs(x="Clinical parameter",y="MicroRNA expression (cpm,log2)") +
          stat_compare_means(method = "anova") 
        #method ="anova"
      })
    }
    
    shinyApp(ui.miRNA.clinical, server.miRNA.clinical)
    

    【讨论】:

      猜你喜欢
      • 2012-12-21
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2021-07-15
      相关资源
      最近更新 更多