一个简单的“paste0”命令在“draw”命令中不起作用? (复杂热图,r)
A Simple `paste0` Command is Not Working Within a `draw` Command? (ComplexHeatmap, r)
作为使用 ComplexHeatmaps 的一部分,我需要创建一个字符串,以便将三张地图绘制在一起。
如果我有热图 A
、B
和 C
,我将需要这样做,
AllMaps <- A + B + C
draw(AllMaps)
这将在一张 canvas.
上绘制所有热图,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
object
draw(eval(parse(text = AllMaps)))
虽然这可行,但通常不推荐使用 eval(parse
。
如果你检查AllMaps
的class
是字符
class(AllMaps)
#[1] "character"
如果你勾选
class(HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']])
#[1] "HeatmapList"
#attr(,"package")
#[1] "ComplexHeatmap"
所以我们需要将这些单独的对象放入 HeatmapList
class。
我们可以使用一个简单的for
循环
HeatmapList = c()
for (i in seq_len(length(Matrices))) {
HeatmapList = HeatmapList + Heatmap(Matrices[[i]], name = "a_name")
}
现在在 HeatmapList
上使用 draw
方法,这将为我们提供预期的输出
draw(HeatmapList)
作为使用 ComplexHeatmaps 的一部分,我需要创建一个字符串,以便将三张地图绘制在一起。
如果我有热图 A
、B
和 C
,我将需要这样做,
AllMaps <- A + B + C
draw(AllMaps)
这将在一张 canvas.
上绘制所有热图,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
object
draw(eval(parse(text = AllMaps)))
虽然这可行,但通常不推荐使用 eval(parse
。
如果你检查AllMaps
的class
是字符
class(AllMaps)
#[1] "character"
如果你勾选
class(HeatmapList[['A']] + HeatmapList[['B']] + HeatmapList[['C']])
#[1] "HeatmapList"
#attr(,"package")
#[1] "ComplexHeatmap"
所以我们需要将这些单独的对象放入 HeatmapList
class。
我们可以使用一个简单的for
循环
HeatmapList = c()
for (i in seq_len(length(Matrices))) {
HeatmapList = HeatmapList + Heatmap(Matrices[[i]], name = "a_name")
}
现在在 HeatmapList
上使用 draw
方法,这将为我们提供预期的输出
draw(HeatmapList)