Shiny 为选定行和临床参数绘制曲线图

Shiny 为选定行和临床参数绘制曲线图,shiny,Shiny,我正在尝试设置一个闪亮的地方,我想为数据中的值做一个方框图,并为“基因”和临床参数(临床中的colnames)创建一个下拉列表。在本例中,临床下拉列表将为TumorLoad和Stage以及Gene1-6 我尝试过breifly,但这可能是错误的方法: library(shiny) ui <- fluidPage( selectInput("variable", "Clinical",choices = names(clinical)),

我正在尝试设置一个闪亮的地方,我想为数据中的值做一个方框图,并为“基因”和临床参数(临床中的colnames)创建一个下拉列表。在本例中,临床下拉列表将为
TumorLoad
Stage
以及
Gene1-6

我尝试过breifly,但这可能是错误的方法:

library(shiny)



ui <- fluidPage(
  selectInput("variable", "Clinical",choices = names(clinical)),
  plotOutput("myplot"))

server <- function(input, output, session) {
  
  output$myplot <- renderPlot({
    
    ggplot(data.m.plot,aes(input$variable,value,fill=input$variable)) + geom_boxplot()
    
    
  })
}

shinyApp(ui, server)
库(闪亮)

ui可以这样实现:

  • 我没有在应用程序内部熔化和连接数据,而是在应用程序外部进行所有数据争用

  • 添加第二个selectintput

  • 使用ggplot进行绘图,其中我使用aes_字符串简单地使用字符输入

  • 试试这个:

    clinical <- data.frame(ID=c("Patient1","Patient2","Patient3","Patient4","Patient5","Patient6"),TumorLoad=c("High","High","High","Low","Low","Low"),Stage=c("1","1","1","3","3","3"))
    
    
    data <- structure(list(Gene = structure(1:6, .Label = c("Gene1", "Gene2", 
                                                            "Gene3", "Gene4", "Gene5", "Gene6"), class = "factor"), Patient1 = c(24, 
                                                                                                                                 42, 42, 4, 24, 24), Patient2 = c(23, 342, 4232, 4, 214, 244), 
                           Patient3 = c(24, 432, 4232, 4, 244, 214), Patient4 = c(424, 
                                                                                  142, 412, 4, 234, 214), Patient5 = c(24, 432, 423, 24, 24, 
                                                                                                                       24), Patient6 = c(24, 432, 412, 4, 23, 241)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                         -6L))
    
    
    library(dplyr)
    library(shiny)
    library(tidyr)
    library(ggplot2)
    
    # Prepare dataset. Make long (melt) and join
    data_prep <- data %>% 
      tidyr::pivot_longer(-Gene, names_to = "ID") %>% 
      left_join(clinical)
    
    
    ui <- fluidPage(
      selectInput("p", "Clinical p", choices = c("TumorLoad", "Stage")),
      selectInput("gene", "Gene", choices = unique(data_prep$Gene)),
      plotOutput("myplot"))
    
    server <- function(input, output, session) {
      
      # filter data by Gene
      data_selected <- reactive({
        filter(data_prep, Gene %in% input$gene)
      })
      
      # Plot. use aes_string to simply use character input p
      output$myplot <- renderPlot({
        ggplot(data_selected(), aes_string(input$p, "value", fill = input$p)) + 
          geom_boxplot()
      })
    }
    
    shinyApp(ui, server)
    
    临床
    
    data.selected <- data[grep("Gene1",data$Gene),] #Dropdown menu for $Gene
    data.m <- melt(data.selected,id.vars=("Gene"))
    data.m.plot <- cbind(clinical,data.m) 
    ggplot(data.m.plot,aes(data.m.plot$TumorLoad,value,fill=data.m.plot$TumorLoad)) + geom_boxplot() #Dropdown menu for clinical, in this example $TumorLoad is plotted
    
    clinical <- data.frame(ID=c("Patient1","Patient2","Patient3","Patient4","Patient5","Patient6"),TumorLoad=c("High","High","High","Low","Low","Low"),Stage=c("1","1","1","3","3","3"))
    
    
    data <- structure(list(Gene = structure(1:6, .Label = c("Gene1", "Gene2", 
                                                            "Gene3", "Gene4", "Gene5", "Gene6"), class = "factor"), Patient1 = c(24, 
                                                                                                                                 42, 42, 4, 24, 24), Patient2 = c(23, 342, 4232, 4, 214, 244), 
                           Patient3 = c(24, 432, 4232, 4, 244, 214), Patient4 = c(424, 
                                                                                  142, 412, 4, 234, 214), Patient5 = c(24, 432, 423, 24, 24, 
                                                                                                                       24), Patient6 = c(24, 432, 412, 4, 23, 241)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                         -6L))
    
    
    library(dplyr)
    library(shiny)
    library(tidyr)
    library(ggplot2)
    
    # Prepare dataset. Make long (melt) and join
    data_prep <- data %>% 
      tidyr::pivot_longer(-Gene, names_to = "ID") %>% 
      left_join(clinical)
    
    
    ui <- fluidPage(
      selectInput("p", "Clinical p", choices = c("TumorLoad", "Stage")),
      selectInput("gene", "Gene", choices = unique(data_prep$Gene)),
      plotOutput("myplot"))
    
    server <- function(input, output, session) {
      
      # filter data by Gene
      data_selected <- reactive({
        filter(data_prep, Gene %in% input$gene)
      })
      
      # Plot. use aes_string to simply use character input p
      output$myplot <- renderPlot({
        ggplot(data_selected(), aes_string(input$p, "value", fill = input$p)) + 
          geom_boxplot()
      })
    }
    
    shinyApp(ui, server)