r - While loop until there is error -


i run piece of code produces error.

for example:

a<-matrix(na,ncol=1,nrow=sample(1:5,1)) a[sample(1:5,1),1]<-10 

i repeate these commands until run without error.

is possible embed while() loop continues running until there error , stops when 2 lines executed without error?

the syntax trycatch looks like

result = trycatch({     expr }, warning = function(warning-condition) {     warning-handler-code }, error = function(error-condition) {     error-handler-code }, finally={     cleanup-code }) 

you can enclose code within infinite loop inside trycatch as

while(1==1) 

these 2 concepts should solve problem. using these, write as

boolfalse<-f while(boolfalse==f) {   trycatch({     a<-matrix(na,ncol=1,nrow=sample(1:5,1))     a[sample(1:6,1),1]<-10;     boolfalse<-t   },error=function(e){   },finally={}) } 

Comments