Rscript:通过循环绘制图像可以提供多个窗口

时间:2015-11-05 17:59:43

标签: r plot

我对使用R脚本非常陌生,但我试图对照片进行更有效的分析。

这是我的脚本,如果我打开文件夹上的每张照片,如果我认为它有问题,我会将文件夹名称与照片名称一起保存在空数据框中并保存。

   #Here I am already within a folder which is dir[i] from another loop
   #type is also another variable from another part of my script
   #starting loop for photos
   for (x in 1:length(pics)) {
      #read photo
      img <- readPNG(pics[x])
      #plot photo
      X11()
      plot(1:10, type="n", axes=F, xlab="", ylab="")
      rasterImage(img,-1,-1,12,12)
      #if photo is bad, press "y"
      n <- readLines("stdin",n=1);
      if (n =="y"){
         row=c(type,dir[i],pics[x])
         DF=rbind(DF,row)
         write.table(DF,file=paste("QCEye_",type,".txt",sep =""),quote=F,row.names = F,col.names = F)
      }
   }

我基本上有两个问题。

第一个是每次我拍摄照片时都会弹出一个新的X11窗口。是否可以在同一个窗口上绘制每张照片?我的文件夹上有500多张图片,所以在某一点上我收到了这个错误:

  

X11()中的错误:打开的设备太多

第二个是每次照片出现时,我必须在终端上“点击”才能给用户输入。是否可以打开照片并让我只是按下终端上的东西来获取下一张照片?

谢谢!

1 个答案:

答案 0 :(得分:0)

这不是问题的完整解决方案,但如果我把它放在这里而不是在评论部分中,它会更容易阅读:

#This should help you with the user input part:
for(i in 1:5){
  QUESTION <- readline("Are you a satisfied with the plot? ")
  # if you are satistified with the plot press "y" or "Y" and hit enter
  if(QUESTION == "y" | QUESTION == "Y"){
    cat("Yes")
  }
}

readline功能可以帮助您保存部分。

for (x in 1:length(pics)) {
  #read photo
  img <- readPNG(pics[x])
  #plot photo
  X11()
  plot(1:10, type="n", axes=F, xlab="", ylab="")
  rasterImage(img,-1,-1,12,12)
  #if photo is bad, press "y"
  QUESTION <- readline("Are you a satisfied with the plot? ")
  if(QUESTION == "y" | QUESTION == "Y"){
    n <- readLines("stdin",n=1);
    if (n =="y"){
       row=c(type,dir[i],pics[x])
       DF=rbind(DF,row)
       write.table(DF,file=paste("QCEye_",type,".txt",sep =""),quote=F,row.names = F,col.names = F)
    }
    dev.off()
  }
  #dev.off() # this may have to go to this part of the loop and not above
}

如果你正在使用RStudio,你可能可以通过移除x11()而逃脱,并且每次都不会打开一个新窗口。你的情节应该出现在RStudio绘图窗口中,然后你就不必担心每次制作情节时新窗口都会变为活动状态。