一个简单的`paste0`命令在`draw`命令中不起作用? (ComplexHeatmap,r)

时间:2019-04-20 01:28:29

标签: r list draw heatmap paste

作为使用ComplexHeatmaps的一部分,我需要创建一个字符串,该字符串将允许我将三个地图绘制在一起。

如果我有热图ABC,则需要这样做,

AllMaps <- A + B + C

draw(AllMaps)

然后将在一张画布上绘制所有ABC的热图。

但是,当我尝试使用热图列表(其中ABC存储在HeatmapList中)时...

    AllMaps <- paste0("HeatmapList[['", names(HeatmapList[1]),
                       "']] + HeatmapList[['", names(HeatmapList[2]), 
                       "']] + HeatmapList[['", names(HeatmapList[3]), 
                       "']]"
                     )

    draw(AllMaps)

失败,并且我收到以下消息:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘draw’ for signature ‘"character"

如果只运行AllMaps,我得到的答案是:

"HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']]"

显示paste0可以正确打印我的对象名称列表。然后,如果我将输出直接复制并粘贴到draw,就可以了!例如

#This works
draw(HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']])

paste0draw的处理方式是什么,我自己运行时却无用?

这是一个示例,如果您想运行它并自己看看:

#Get the most recent ComplexHeatmaps Package
library(devtools)
install_github("jokergoo/ComplexHeatmap", force = TRUE)
library(ComplexHeatmap)


#Make Example Matrices
Matrices = list()

Matrices[['Mtx1']] <- matrix(  c(2, 4, 5, 7), nrow=2, ncol=2, dimnames = list(c("Row1", "Row2"), c("C.1", "C.2"))) 
Matrices[['Mtx2']] <- matrix(  c(5, 1, 3, 9), nrow=2, ncol=2, dimnames = list(c("Row1", "Row2"), c("C.1", "C.2"))) 
Matrices[['Mtx3']] <- matrix(  c(8, 3, 7, 5), nrow=2, ncol=2, dimnames = list(c("Row1", "Row2"), c("C.1", "C.2"))) 

#Create Heatmaps
HeatmapList = c()

HeatmapList <- lapply(Matrices, function(q) {
  Heatmap(q, name = "a_name") 
})

names(HeatmapList) <- c('A', 'B', 'C')

#Draw a heatmap to check it's all working
draw(HeatmapList[[2]])

#Create Heatmap string so A, B and C can all be plotted together
AllMaps <- (paste0("HeatmapList[['", names(HeatmapList[1]), "']] + ",
                   "HeatmapList[['", names(HeatmapList[2]), "']] + ",
                   "HeatmapList[['", names(HeatmapList[3]), "']]" ))

#Draw using the string we just made --> DOESN"T WORK!
draw(AllMaps)

#Check the string --> LOOKS FINE, JUST AS IT SHOULD BE 
paste0(AllMaps)

# Copy and paste string manually into draw command --> THIS WORKS
draw(HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']])

#SO WHY DOES THIS FAIL???
draw(AllMaps)

1 个答案:

答案 0 :(得分:1)

AllMaps只是一个纯字符串,当您在draw函数中传递它而不是将其作为HeatmapList对象时,它会将其评估为字符,因此会给出错误消息。一种选择是使用eval(parse(text将字符串评估为HeatmapList对象

draw(eval(parse(text = AllMaps)))

尽管这可行,但是通常不建议使用eval(parse

如果您选中class中的AllMaps,则为字符

class(AllMaps)
#[1] "character"

,如果您选中

class(HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']])

#[1] "HeatmapList"
#attr(,"package")
#[1] "ComplexHeatmap"

所以我们需要将那些单独的对象带入HeatmapList类中。

我们可以使用简单的for循环

HeatmapList = c()

for (i in seq_len(length(Matrices))) {
  HeatmapList = HeatmapList + Heatmap(Matrices[[i]], name = "a_name") 
}

,现在在draw上使用HeatmapList方法,它将为我们提供预期的输出结果

draw(HeatmapList)

enter image description here