Continue loop execution even if a pass gives problem
I am downloading Bovespa stock data by Package quantmod
.
However, I still do not know why, in the Santander data (SANB11) the getSymbols
function of the package is giving problem and the loop for execution. Do you have a way to make the loop continue execution 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")
}
5
Author: Rafael Cunha, 2014-02-19
1 answers
Use the function try
. 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 evaluation, preventing script execution from being interrupted because of the error.
If you need to treat the error, use the function tryCatch
.
6
Author: rodrigorgs, 2014-02-20 16:33:46