[英]retreive Seurat object name in R during a for loop
I'm working on single cell rna-seq on Seurat and I'm trying to make a for() loop over Seurat objects to draw several heatmaps of average gene expression.我正在研究 Seurat 上的单细胞 rna-seq,我正在尝试在 Seurat 对象上创建一个 for() 循环,以绘制几个平均基因表达的热图。
for(i in c(seuratobject1, seuratobject2, seuratobject3)){
cluster.averages <- data.frame(AverageExpression(i, features = genelist))
cluster.averages$rowmeans <- rowMeans(cluster.averages)
genelist.new <- as.list(rownames(cluster.averages))
cluster.averages <- cluster.averages[order(cluster.averages$rowmeans),]
HMP.ordered <- DoHeatmap(i, features = genelist.new, size = 3, draw.lines = T)
ggsave(HMP.ordered, file=paste0(i, ".HMP.ordered.png"), width=7, height=30)
the ggsave line does not work as it takes i as a seurat object. ggsave 行不起作用,因为它将 i 作为 seurat object。 Hence my question: How to get ggsave() to use the name of my seurat object stored in "i"?
因此我的问题是:如何让 ggsave() 使用存储在“i”中的我的 seurat object 的名称?
I tried substitute(i) and deparse(substitute(i)) w/o success.我尝试了 substitute(i) 和 deparse(substitute(i)) 没有成功。
Short answer: you can't.简短的回答:你不能。
Long answer: using substitute
or similar to try to get i
's name will give you … i
.长答案:使用
substitute
或类似来尝试获取i
的名字会给你... i
。 (This is different for function arguments , where substitute(arg)
gives you the call's argument expression.) (这与function arguments不同,其中
substitute(arg)
为您提供调用的参数表达式。)
You need to use a named vector instead.您需要改用命名向量。 Ideally you'd have your Seurat objects inside a list to begin with.
理想情况下,您应该将 Seurat 对象放在一个列表中。 But to create such a list on the fly, you can use
get
:但是要即时创建这样的列表,您可以使用
get
:
names = c('seuratobject1', 'seuratobject2', 'seuratobject3')
for(i in names) {
cluster.averages <- data.frame(AverageExpression(get(i), features = genelist))
# … rest is identical …
}
That said, I generally advocate strongly against the use of get
and for treating the local environment as a data structure.也就是说,我通常强烈反对使用
get
并将本地环境视为数据结构。 Lists and vectors are designed to be used in this situation instead.列表和向量被设计用于这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.