我可以在R中创建一个for循环来保存图像吗?

时间:2020-05-14 19:31:27

标签: r image for-loop

我正在尝试运行一个简单的循环,该循环包括1)运行模型和2)保存图。 我最终要跑很多!

我遇到的问题是这个。为了通过我使用的方法保存绘图,我必须像下面这样“调用”绘图:

png(file=paste(ImageDirectory,"\\","i0.png",sep=""), width = 1000, height = 500)
i0
dev.off()

成功。 但是,在循环中,不会渲染图。相反,R只是将打印名称打印为字符串。

# Attempted and Failed Loop
images <- c("i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "i10", "i11")
for (i in images){
  png(file=paste(ImageDirectory,"\\",i,".png",sep=""), width = 1000, height = 500)
  i
  dev.off()
  print(typeof(i))
}
[1] "character"

结果,图像调用将导致空白图像。 我知道为什么会这样。我已经将图像的名称存储为文本。我尝试过:

as.data.frame(i)

不高兴!很抱歉,如果有人提出并回答了这个问题,我一点都找不到。这个question接近了,但对我来说也不成功。

下面的完整代码序列。任何建议将是非常赞赏!

#Run Survival Models
survival_model <- Surv(time = df$LOS, event = df$isTermed)
fit1 <- survfit(survival_model ~ rResult, data = df)
i0<-ggsurvplot(fit1, data = df,title=df$Label,legend.title = "Result",legend.labs = c("Not Recommended", "Recommended", "Not Assessed"),pval = TRUE,conf.int = FALSE,palette = c("#C00000","#92D050", "#FFC000"),linetype="solid",ggtheme = theme_classic(),break.time.by=90,xlab = "Time in Days",ylab = "Probability of Retention") 
pvals<-rbind(pvals, list("pvals"=surv_pvalue(fit1)$pval.txt),stringsAsFactors=FALSE)

survival_model <- Surv(time = df1$LOS, event = df1$isTermed)
fit1 <- survfit(survival_model ~ rResult, data = df1)
i1<-ggsurvplot(fit1, data = df1,title=df1$Label,legend.title = "Result",legend.labs = c("Not Recommended", "Recommended"),pval = TRUE,conf.int = FALSE,palette = c("#C00000","#92D050", "#FFC000"),linetype="solid",ggtheme = theme_classic(),break.time.by=90,xlab = "Time in Days",ylab = "Probability of Retention") 
pvals<-rbind(pvals, list("pvals"=surv_pvalue(fit1)$pval.txt),stringsAsFactors=FALSE)

survival_model <- Surv(time = df2$LOS, event = df2$isTermed)
fit1 <- survfit(survival_model ~ rResult, data = df2)
i2<-ggsurvplot(fit1, data = df2,title=df2$Label,legend.title = "Result",legend.labs = c("Not Recommended", "Recommended", "Not Assessed"),pval = TRUE,conf.int = FALSE,palette = c("#C00000","#92D050", "#FFC000"),linetype="solid",ggtheme = theme_classic(),break.time.by=90,xlab = "Time in Days",ylab = "Probability of Retention") 
pvals<-rbind(pvals, list("pvals"=surv_pvalue(fit1)$pval.txt),stringsAsFactors=FALSE)

#Save their plots
png(file=paste(ImageDirectory,"\\","i0.png",sep=""), width = 1000, height = 500)
i0
dev.off()

png(file=paste(ImageDirectory,"\\","i1.png",sep=""), width = 1000, height = 500)
i1
dev.off()

png(file=paste(ImageDirectory,"\\","i2.png",sep=""), width = 1000, height = 500)
i2
dev.off()
png(file=paste(ImageDirectory,"\\","i3.png",sep=""), width = 1000, height = 500)


# Attempted and Failed Loop
images <- c("i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "i10", "i11")
for (i in images){
  png(file=paste(ImageDirectory,"\\",i,".png",sep=""), width = 1000, height = 500)
  i
  dev.off()
}

2 个答案:

答案 0 :(得分:1)

我还没有尝试过(因为目前无法尝试),但是我认为是因为

i0 <- ggsurvplot(
    fit1,
    data = df,
    title=df$Label, ...)

不返回图。而是,您得到数据类型字符。例如,尝试

typeof("MyName")

将绘图逻辑移动到for循环中。这将需要将您的数据帧保存到列表中,然后遍历该列表。例如

my.dfs <- list(
  df.name.one = df.name.one,
  df.name.two = df.name.two,
  ....)

for ( i in length(my.dfs) ){
  png(file=paste(ImageDirectory,"\\", names(my.dfs)[i] ,".png",sep=""), width = 1000, height = 500)

  survival_model <- Surv(time = my.dfs[i]$LOS, event = my.dfs[i]$isTermed)
  fit <- survfit(survival_model ~ rResult, data = df)
  ggsurvplot(fit, ....)

  dev.off()
}

您可以进一步考虑使用apply系列中的函数,并将建模和绘图逻辑移入函数中。

我希望这会有所帮助!

答案 1 :(得分:1)

您主要需要get()

images <- c("i1", "i2", "i3", "i4", "i5", "i6", "i7", "i8", "i9", "i10", "i11")
for (i in images){
  png(file=paste(ImageDirectory,"\\",i,".png",sep=""), width = 1000, height = 500)
  get(i) ## may have to print, too:. print(get(I))
  dev.off()
}

似乎最好使用某种list()

l <- mget(ls(pattern = "df"))
lapply(seq_along(l),
    function (i) {

     DF <- l[[i]]
      survival_model <- Surv(time = DF$LOS, event = DF$isTermed)
      fit <- survfit(survival_model ~ rResult, data = DF)

      png(file=paste(ImageDirectory,"\\",images[i],".png",sep=""), width = 1000, height = 500)

      ggsurvplot(fit, data = DF,title=DF$Label,legend.title = "Result",legend.labs = c("Not Recommended", "Recommended"),pval = TRUE,conf.int = FALSE,palette = c("#C00000","#92D050", "#FFC000"),linetype="solid",ggtheme = theme_classic(),break.time.by=90,xlab = "Time in Days",ylab = "Probability of Retention") 

      return("pvals"=surv_pvalue(fit)$pval.txt)
    }
 )