Continue running the loop even if a passage gives problem

Asked

Viewed 363 times

5

I’m downloading Bovespa’s stock data for the package quantmod.

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

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

1 answer

6


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

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

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

If you need to treat the error, use the function tryCatch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.