R语言实用技巧--用get函数配合dplyr包传参

使用场景,今天在做cellchat,需要提取一下几种细胞类型的id。目前的设置是这样。

R 复制代码
sce.all = seurat_obj
Idents(sce.all) = "CAFs"
celltype_sender=c("iCAF","myCAF")

如果正常写代码传参是这样

R 复制代码
sender_ids <- sce.all@meta.data %>% 
    filter(CAFs %in% celltype_sender) %>%
    rownames()

但是如果我想换一种细胞类型,CAFs就也要改,不够简洁。filter函数是不支持以下写法的,什么都筛选不出来

R 复制代码
celltype_sender_idents = "CAFs"
sender_ids <- sce.all@meta.data %>% 
    filter(celltype_sender_idents %in% celltype_sender) %>%
    rownames()

解决方案:

R 复制代码
sce.all = seurat_obj
Idents(sce.all) = "CAFs"
celltype_sender=c("iCAF","myCAF")
celltype_sender_idents = "CAFs"

sender_ids <- sce.all@meta.data %>% 
    filter(get(celltype_sender_idents) %in% celltype_sender) %>%
    rownames()

这样就可以在一开始把变量设置好,在后面优雅的传参了

相关推荐
小付同学呀42 分钟前
C语言学习(八)——C判断(switch语句)
c语言·学习·算法
科技林总1 小时前
【系统分析师】11.6 软件需求确认和验证
学习
ZTLJQ2 小时前
深入理解Autoencoder:无监督学习的异常检测与数据压缩利器
学习
夏星印2 小时前
argparse解析器参数详解
经验分享·笔记·python·学习·argparse
Shining05962 小时前
AI编译器系列(二)《AI 编译器中的前端优化》
人工智能·学习·其他·学习方法·infinitensor
码喽7号3 小时前
springboot学习四:RESTful风格+swagger
spring boot·学习·restful
weixin_458872613 小时前
东华复试OJ二刷复盘8
学习
海奥华23 小时前
Rust初步学习
开发语言·学习·rust
王知无(import_bigdata)4 小时前
一个极简的AI Agentic Engineering技术栈学习路线
人工智能·学习