我正在使用软件包complexheatmap
在脚本中,我需要创建一个变量ha_column,并将其合并到脚本中。
ha_column = HeatmapAnnotation (df = data.frame(type1=c(rep("name1",5), rep("name2",5),rep("name3",5), col = list(type1=c("name1" = "#DCDCDC", "name2" = "#DC928B", "name2"="#BA72D3")))))
我有2个向量:
vectors1=c("name1","name2","name3)
vectors2=c("#DCDCDC","#DC928B","#BA72D3")
,其想法是使用这两个向量重现上述脚本。
我尝试过:
paste0("ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(",vectors1,", 5),col = list(type1 = c(",vectors1,"=",vectors2,")))")
仅粘贴一行,例如:
[1] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name1, 5),col = list(type1 = c(name1=#DCDCDC)))"
[2] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name2, 5),col = list(type1 = c(name2=#DC928B)))"
[3] "ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep(name3, 5),col = list(type1 = c(name3=#BA72D3)))"
代替我想做的...
有人有想法吗?
感谢您的时间。
答案 0 :(得分:2)
将代码构建为字符串通常不是一个好主意。而是考虑构建一个功能来完成您想要的事情。
您可以做一些事情
ha_column_fun = function(names, colors) {
HeatmapAnnotation(
df = data.frame(type1 = rep(names, each=5)),
col = list(type1=setNames(colors, names))
)
}
然后您可以使用
进行调用ha_column = ha_column_fun(vectors1, vectors2)