简体   繁体   中英

Rscript: Plotting images through a loop gives multiple windows

I am very new to using R script, but I was trying to make an analysis of photos more effective.

This is my script, were I open every photo on a folder, and if I decide it has a problem I save the folder name together with the name of the photo on an empty dataframe and save it.

   #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)
      }
   }

I basically have two problems.

The first one is that every time I plot a photo a new X11 windows pops up. Is it possible for every photo to be plotted on the same windows? I have more than 500 pictures on my folders so at a point I get this error:

Error in X11() : too many open devices

The second one is every time the photo appears, I have to "click" on the terminal in order to give the user input. Is it possible to open the photo and still let me just press something on the terminal to get the next one?

Thank you!!

This isn't the complete solution to your problem but it will be easier to read if I put it down here rather than in the comments section:

#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")
  }
}

The readline function should help you with the saving part.

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
}

If you're using RStudio you might be able to get away with removing the x11() and a new window won't open up each time. your plot should show up in the RStudio plotting window and then you won't have to worry about a new window becoming active each time a plot is made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM