0

我目前正在开发一个应用程序,该应用程序使用 ComplexHeatmap 和 InteractiveComplexHeatmap 包来显示热图,然后允许用户刷并创建新的热图。热图使用 k-means 进行聚类,如果用户刷过两个集群(跨越集群之间的间隙),则只有一个集群中的行将显示在子热图中。下面的代码演示了这个问题。

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      NULL
    ),
    mainPanel(
      plotOutput(
        outputId = "heatmap_example_main",
        width = "100%",
        height = "600px",
        brush = "ht_brush"
      ),
      plotOutput(
        outputId = "heatmap_example_sub",
        width = "100%",
        height = "600px"
      )
    )
  )
)

server <- function(input, output) {
  shiny_env <- new.env()

  output$heatmap_example_main <- renderPlot({
    iris <- as.matrix(iris[1:4])

    shiny_env$heat <- ComplexHeatmap::draw(ComplexHeatmap::Heatmap(
      iris,
      row_km = 3,
      row_dend_side = "left",
      row_dend_width = grid::unit(1, "cm"),
      show_row_names = FALSE
    ))

    shiny_env$heat_pos <- InteractiveComplexHeatmap::htPositionsOnDevice(
      shiny_env$heat
    )

    return(shiny_env$heat)
  })

  output$heatmap_example_sub <- renderPlot({

    if (is.null(input$ht_brush)) {
      grid::grid.newpage()
      grid::grid.text("No region is selected.", 0.5, 0.5)
    } else {
      lt <- InteractiveComplexHeatmap::getPositionFromBrush(input$ht_brush)
      pos1 <- lt[[1]]
      pos2 <- lt[[2]]
  
      ht <- shiny_env$heat
      pos <- InteractiveComplexHeatmap::selectArea(
        ht,
        mark = FALSE,
        pos1 = pos1,
        pos2 = pos2,
        verbose = FALSE,
        ht_pos = shiny_env$heat_pos
      )
  
      row_index <- unlist(pos[1, "row_index"])
      column_index <- unlist(pos[1, "column_index"])
      m <- ht@ht_list[[1]]@matrix
      shiny_env$submap <- m[row_index, column_index, drop = FALSE]
  
      ht_select <- ComplexHeatmap::Heatmap(
        shiny_env$submap,
        col = ht@ht_list[[1]]@matrix_color_mapping@col_fun,
        show_heatmap_legend = FALSE,
        cluster_rows = FALSE,
        cluster_columns = FALSE,
        show_row_names = FALSE
      )
  
      ComplexHeatmap::draw(ht_select)
    }
  })
}

shinyApp(ui, server)

有谁知道如何解决这个问题?

4

0 回答 0