Geoquery: How to Continue Code Execution on Error
1
0
Entering edit mode
r.b.li • 0
@rbli-7122
Last seen 7.4 years ago
China

I have written an R script which includes a loop to download a list of Gsm. However, the url of some Gsms have changed or the condition internet became bad temporarily leading to a break of loop.

I want to continue the loop if some gsms fail to download and the name of gsm is written in a new list.

Thank you in advance.

geoquery • 5.5k views
1
Entering edit mode
Robert Castelo ★ 2.8k
@rcastelo
Last seen 18 days ago
Barcelona/Universitat Pompeu Fabra

Hi,

I would say this is a general question about error handling in R, which can be handled with 'tryCatch()'. If you Google for 'r-project tryCatch'. The second hit gives hints about your specific question at http://stackoverflow.com/questions/12193779/how-to-write-trycatch-in-r.

Just to add a straightforward example, if you type in R:

download.file("http://thisurldoesnotexist.com/missing.tar.gz", "missing.tar.gz")

you'll get an error and the script that contains this instruction will stop. However if you do it this way:

tryCatch({download.file("http://thisurldoesnotexist.com/missing.tar.gz", "missing.tar.gz")}, error=function(err) { warning("file could not be downloaded") })

the error is handled in the functtion provided as second argument, which could decide to stop the script but in this case it just issues a warning. The pointer given before contains a more complete and sophisticated solution.

cheers,

robert.

0
Entering edit mode

Hi, Robert.

Thanks for your kind help.

I am new in R and it is a lot hard for me. Can you help me identify where my code listing below will be working?

testlist=c("GSM1248685","x","GSM1016398")

recordlist=c()

for (i in c(1,2,3)){
acc <- testlist[i]
tryCatch(getGEOfile(acc, destdir = address, AnnotGPL = F), error=function(err){recordlist=append(recordlist, testlist[i])})
}

Thanks a lot~

Best regards

Robin

0
Entering edit mode

hi Robin,

I'm not sure I understand what you're asking with "Can you help me identify where my code listing below will be working?". If you mean "whether my code will be working?" then I would suggest you to try it out yourself. If it does not work, try to interpret the error and the output and if you need hints about how to proceed, paste here the output of the execution and me and other people may be able to help.

cheers,

robert.