[英]Trouble correctly returning object name in for loop
I'm trying to automate making a series of the same plot using different objects; 我正在尝试使用不同的对象自动制作一系列相同的情节。 I'm working with S4 class phyloseq objects.
我正在使用S4类phyloseq对象。 When I use a for loop to iterate over a list of objects and try to use the object name as a title for each plot and in a filename for ggsave I can't quite get it to recognize the correct name, though it's making the correct plots for a given object in the list.
当我使用for循环遍历对象列表并尝试将对象名称用作每个图的标题以及ggsave的文件名时,尽管它可以正确地进行识别,但我无法完全识别出正确的名称列表中给定对象的绘图。
I've tried using variations of deparse(substitute(object))
with get()
and quote()
and end up getting slightly different, but still off-target results. 我尝试使用
get()
和quote()
的deparse(substitute(object))
变体,但最终得到的结果略有不同,但仍然偏离目标。
object_list <- c(object1, object2, object3)
automate_graphs <- function(x){
for(object in x){
name <- deparse(substitute(object))
ordination <- ordinate(object, "NMDS", "bray")
plot <- plot_ordination(object, ordination) + ggtitle(label = name)
ggsave(plot, filename=sprintf("NMDS_bray_%s.pdf", name), height=4, width=7)}}
automate_graphs(object_list)
I'm expecting to save 3 pdfs named NMDS_bray_object1
, NMDS_bray_object2
, NMDS_bray_object3
. 我预计要保存3个名为
NMDS_bray_object1
, NMDS_bray_object2
, NMDS_bray_object3
pdf NMDS_bray_object3
。
Instead I get NMDS_bray_S4 object of class structure("phyloseq", package = "phyloseq")
(so it's saving the deparse of the object to the variable name rather than the substitution) or with quote I get NMDS_bray_object
which I suppose is to be expected haha. 相反,我得到
NMDS_bray_S4 object of class structure("phyloseq", package = "phyloseq")
(因此它将对象的稀疏保存为变量名而不是替换名)或使用引号得到了NMDS_bray_object
,我认为这是可以预期的哈哈。 Thanks in advance for any help! 在此先感谢您的帮助!
Just make it a named vector (list) of objects and iterate over the names: 只需使其成为对象的命名向量(列表)并遍历名称即可:
object_list <- c(object1 = object1,object2 = object2,object3 = object3)
automate_graphs <- function(x){
for(nm in names(x)){
object <- x[nm] #Pick out the one named nm
ordination <- ordinate(object, "NMDS", "bray")
plot <- plot_ordination(object, ordination) + ggtitle(label = nm)
ggsave(plot, filename=sprintf("NMDS_bray_%s.pdf", nm), height=4, width=7)
}}
automate_graphs(object_list)
@Joran thanks again for the help--it pushed me in the right direction to figuring out a solution, even if it's not the most elegant. @Joran再次感谢您的帮助-即使不是最优雅的解决方案,它也使我朝着正确的方向提出了解决方案。 I took the idea of generating a vector of names and then just created an extra variable to cycle through that vector.
我想到了生成名称向量的想法,然后只是创建了一个额外的变量来循环遍历该向量。 But this way it maintains the class of object and creates a separate list of corresponding names:
但是通过这种方式,它可以维护对象的类并创建一个单独的对应名称列表:
object_list <- c("object1" = object1, "object2" = object2, "object3" = object3)
automate_graphs <- function(x){
names = names(x)
obj_num = 1
for(object in x){
name <- names[obj_num]
ordination <- ordinate(object, "NMDS", "bray")
plot <- plot_ordination(object, ordination) + ggtitle(label = name)
ggsave(plot, filename=sprintf("NMDS_bray_%s.pdf", name), height=4, width=7)
obj_num = obj_num + 1
}
}
automate_graphs(object_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.