break out and "break in" a loop again


#1

Hello!
I was wondering whether it is possible to interrupt an ongoing loop at a specific point so that a user can enter other commands, before going on with the rest of the loop when he/she is done.
This is because every iteration would need a quick overview of the produced graph and eventually its modification prior to the remaining analysis.
So far I have unsuccessfully made tries with the readline() function, but either I am not using it properly or it is just not suited for what I aim to.
Thanks for your help!


#2

hi,

not sure exactly to understand your problem! if you know what's the condition you can use if + readline to get the user input & then execute the end of the loop. But which user inputs do you expect? I mean shiny maybe more indicated for that!

here is an ugly example (sorry) that waits user choice to print the values or draw the histogram and then execute the remaining part of the loop

test <- data.frame(a = rnorm(50), b = rnorm(50, 10, 3), c = rnorm(50, 2, 1))

for (i in seq_along(test)) {
  if (i == 2) {
    sel <- readline(prompt = "1 histo else values: ")
    res <- hist(x = test[[i]], plot = FALSE)
    if (sel == 1) {
      plot(res)
      Sys.sleep(2)
    } else {
      print(res$counts)
    }
  }
  plot(x = 1:50, y = test[[i]], pch = i, main = i)
  Sys.sleep(2)
}

#3

Unfortunately one does not know the condition in advance. I'd better post the case.

for(i in seq_along(images)[-1]){
  seg<-segment(images[i], display=FALSE, filter = myfilter)
  quartz()  
  regi<-registration(images[i], coordinate=coord[i], display=TRUE, filter = myfilter)
  dev.copy(pdf, paste0(tools::file_path_sans_ext(basename(images[i])), 'regi.pdf'))
}

I need the loop to stop before saving regi as pdf (I skipped the following commands of the loop for simplicity), because regi itself is likely to be modified with

regi<-change.corrpoints()

before running again

regi<-registration(images[i], coordinate=coord[i], display=TRUE, filter = myfilter, correspondance = regi)

Only now one would need to save produce a pdf file.
Thanks for replying!


#4

humm ok! i should say that if you don't know the condition in advance i can't see how to do it... the only thing would be to ask the user at each iteration with readline and according to the input modify or not with you change.corrpoints() but it would required an action of the user at each step of the loop!

maybe someone will come up with a better idea! anyway, good luck !


closed #5

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.