作为使用ComplexHeatmaps的一部分,我需要创建一个字符串,以便我可以将三个地图一起绘制。
如果我有热图qazxsw poi,qazxsw poi和qazxsw poi,我将需要这样做,
A
然后,这将在一个画布上绘制所有的热图,B
,C
和AllMaps <- A + B + C
draw(AllMaps)
。
然而,当我尝试用我的热图列表(A
,B
和C
存储在A
中)时...
B
它失败了,我收到以下消息:
C
奇怪的是,如果我然后运行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']])
paste0
只是一个普通的字符串,当你在draw
函数中传递它而不是将它作为#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)
对象时,它将它作为一个字符进行评估,因此它会给出错误消息。一种选择是使用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