我对为什么这段代码不断抛出错误感到非常困惑。
qc_and_normalize <- function(seurat_object, coeff=3) {
nCount_RNA_upper <- median(seurat_object@meta.data$nCount_RNA) + coeff*mad(seurat_object@meta.data$nCount_RNA)
nFeature_RNA_upper <- median(seurat_object@meta.data$nFeature_RNA) + coeff*mad(seurat_object@meta.data$nFeature_RNA)
nCount_RNA_lower <- median(seurat_object@meta.data$nCount_RNA) - coeff*mad(seurat_object@meta.data$nCount_RNA)
nFeature_RNA_lower <- median(seurat_object@meta.data$nFeature_RNA) - coeff*mad(seurat_object@meta.data$nFeature_RNA)
dims <- seurat_object@assays$RNA@counts@Dim
dims_lower_cut <- sum((seurat_object$nCount_RNA < nCount_RNA_lower) & (seurat_object$nFeature_RNA < nFeature_RNA_lower))
dims_upper_cut <- sum((seurat_object$nCount_RNA > nCount_RNA_upper) & (seurat_object$nFeature_RNA > nFeature_RNA_upper))
print(paste0('removing ', round((dims_lower_cut + dims_upper_cut) / dims[2] * 100, 2), '% of samples'))
# QC
# remove "bad cells"
seurat_object <- subset(seurat_object, ( nFeature_RNA > nFeature_RNA_lower | nCount_RNA > nCount_RNA_lower)
& (nCount_RNA < nCount_RNA_upper | nFeature_RNA < nFeature_RNA_upper))
# normalize data
# by default, normalization.method = "LogNormalize", scale.factor = 10000
seurat_object <- NormalizeData(seurat_object)
return(seurat_object)
}
功能没那么重要,到了线就行了
seurat_object <- subset(seurat_object, ( nFeature_RNA > nFeature_RNA_lower | nCount_RNA > nCount_RNA_lower)
& (nCount_RNA < nCount_RNA_upper | nFeature_RNA < nFeature_RNA_upper))
它抛出错误
Error in eval(expr = expr) : object 'nFeature_RNA_lower' not found
nFeature_RNA_lower
为什么在函数中定义时找不到?当我尝试在错误行上方打印一行时,它会毫无问题地打印它。当我在全局环境中逐行运行代码时,一切正常,但是当我将它变成一个函数时,它突然找不到nFeature_RNA_lower
.
这非常令人困惑,有人可以帮助我理解吗?谢谢!