Continue to execute the loop even if a passage gives a problem

5

I am downloading the Bovespa stock data for the quantmod package.

However, I still do not know why, in the data from Santander (SANB11) the getSymbols function of the package is giving problem and the loop for execution. Is there a way to make the loop continue to execute even when a loop step failed?

library(quantmod)    
tickers<-c("OIBR4.SA", "PCAR4.SA", "PDGR3.SA", "PETR3.SA", "PETR4.SA", "RENT3.SA", 
           "RSID3.SA", "SANB11.SA", "SBSP3.SA", "SUZB5.SA", "TIMP3.SA", 
           "TRPL4.SA", "UGPA3.SA", "USIM3.SA", "USIM5.SA", "VAGR3.SA", "VALE3.SA", 
           "VALE5.SA", "VIVT4.SA")

for (i in tickers){
getSymbols(i,src="yahoo")
}
    
asked by anonymous 20.02.2014 / 00:31

1 answer

6

Use the try function. Your code will look like this:

for (i in tickers) {
  try(getSymbols(i,src="yahoo"))
}

The try function evaluates the expression passed as a parameter and captures any errors that occur during the evaluation, preventing script execution from being interrupted because of the error.

If you need to handle the error, use the tryCatch .

    
20.02.2014 / 00:55