【问题标题】:How to plot a matrix with user-chosen parameters? R Shiny如何使用用户选择的参数绘制矩阵? R闪亮
【发布时间】:2019-06-10 03:35:54
【问题描述】:

我正在尝试制作一个闪亮的应用程序,它将为选定的患者绘制感兴趣的基因。每行是基因名称,每列是患者 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 选择SKI 和TTlLL10,我将如何进行绘制?

【问题讨论】:

    标签: r plot shiny shiny-reactivity


    【解决方案1】:

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

    我所做的更改:

    • input$geneExp 和 input$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)
    
    
    

    【讨论】:

    • 我刚刚选择了一个水平图作为练习图,您认为有没有更好的方法来可视化这些数据?
    • 抱歉,除了马赛克图,我想不到任何其他的了。如果这已经解决了问题,请将其标记为已回答。也可以随意发布关于可视化类型的另一个问题,您可能会得到更好的结果。
    • 你是个传奇!!解决了问题,感谢您的建议:D
    猜你喜欢
    • 2020-04-14
    • 2020-05-08
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多