如何使用用户选择的参数绘制矩阵? R闪亮

How to plot a matrix with user-chosen parameters? R Shiny

我正在尝试制作一个 Shiny 应用程序,它可以为选定的患者绘制感兴趣的基因。每行是基因名称,每列是患者 ID。例如:

        99901    99902  99903   99904
SKI     4.789    5.789  6.324   1.2222
VWA1    6.901    7.002  5.89    4.567
TTLL10  6.783    7.345  8.987   6.345

library(shiny)
library(shinythemes)
library(lattice)

anno <- as.matrix(anno_genExp_gen3[1:3, 1:3])

#Define UI
ui <- fluidPage(
    sidebarPanel(
      titlePanel(title = "Gen3 Gene Expression Data"),
      selectInput(inputId = "patients", 
                  label = strong("Please choose patient/s to examine"),
                  choices = colnames(anno_genExp_off[,1:25]), 
                  multiple = TRUE),
      selectInput(inputId = "geneExp",
                  label = "Please select gene expressions/s to examine",
                  choices = rownames(anno_genExp_off[1:25,]), 
                  multiple = TRUE)),
    mainPanel(plotOutput("testPlot"))  
    )

server <- function(input, output) {
  pdata <- reactive(input$patients)
  gdata <-reactive(input$geneExp)
  output$testPlot <- renderPlot ({
    levelplot(anno, 
              col.regions=colorRampPalette(c("red","green","blue")))
  })
}

shinyApp(ui = ui, server = server)

上面的代码只是绘制了一个小矩阵,但我如何让它使用反应性来绘制用户输入?

如果用户只为患者 99901 选择 SKITTlLL10,我将如何绘制此图?

如上所述,我自己创建了一个示例数据框。这是修改后的代码。

我所做的更改:

  • input$geneExpinput$patients 已经是反应式的,因此不需要使用单独的反应式函数。

  • 使用相同的方法过滤绘图数据框

  • 此外,在 selectInput 中设置了默认值 selected 以避免在未选择任何内容时出现初始错误消息

library(shiny)
library(shinythemes)
library(lattice)

anno_genExp_off <- data.frame(`99901` = c(4.3,6.5,6.6),
                              `99902` = c(5.3,7.5,8.6),
                              `99903` = c(6.3,8.5,9.6),
                              row.names = c("SKI","VWA1","TTLL10"))


anno <- as.matrix(anno_genExp_off)

#Define UI
ui <- fluidPage(
  sidebarPanel(
    titlePanel(title = "Gen3 Gene Expression Data"),
    selectInput(inputId = "patients", 
                label = strong("Please choose patient/s to examine"),
                choices = colnames(anno_genExp_off), 
                selected = colnames(anno_genExp_off)[1],
                multiple = TRUE),
    selectInput(inputId = "geneExp",
                label = "Please select gene expressions/s to examine",
                choices = rownames(anno_genExp_off), 
                selected = rownames(anno_genExp_off)[1],
                multiple = TRUE)),
  mainPanel(plotOutput("testPlot"))  
)

server <- function(input, output) {
  #pdata <- reactive(input$patients)
  #gdata <-reactive(input$geneExp)


  output$testPlot <- renderPlot ({
    levelplot(x = as.matrix(anno_genExp_off[which(rownames(anno_genExp_off) %in% input$geneExp) ,input$patients]), 
              col.regions=colorRampPalette(c("red","green","blue")))
  })
}

shiny::shinyApp(ui,server)