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

时间:2019-01-16 04:32:30

标签: r plot shiny shiny-reactivity

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

import _ from 'lodash';

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

如果用户仅为患者 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) 选择SKITTlLL10,我将如何进行绘制?

1 个答案:

答案 0 :(得分:0)

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

我所做的更改:

  • input$geneExpinput$patients已经是反应性的,因此无需使用单独的反应性功能。

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

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

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)


enter image description here