[R-SIG-Finance] TryCatch and continuing within a loop

Joshua Ulrich josh.m.ulrich at gmail.com
Tue Mar 25 14:23:30 CET 2014


Ralph,

This isn't really on-topic for R-SIG-Finance, so please respond to me
off-list if you have any questions.

The bad news: I'm not sure how to do what you want with a try-catch
block.  I can see why you tried this (given your Java experience) but
the good news is that it's not necessary.  The solution below should
work and should also be faster.

# vector of symbols
brsym <- c("^BVSP", "^GSPTSE", "^N225", "CAC4L.NX", "^BSESN")
# paste is vectorized, no need to loop
j <- "http://finance.yahoo.com/d/quotes.csv?s="
j <- paste0(j, paste0(brsym,collapse="+"), "&f=sd1ohgl1vqd")
# use na.strings arg to convert "N/A" to R's NA
X <- read.csv(j, header=FALSE, as.is=TRUE, na.strings="N/A")
# store rows with missing dates in new object
badsymbols <- X[is.na(X$V2),]
# remove rows with missing dates from 'X'
X <- X[!is.na(X$V2),]
# reformat date
X$V2 <- format(as.Date(X$V2, "%m/%d/%Y"), "%Y%m%d")
# paste into one string per row
Y <- with(X, paste(paste(V1, "-", V2), V3, V4, V5, V6, V7, sep=","))
# write to file

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com


On Mon, Mar 24, 2014 at 5:10 PM, R Vince <rvince99 at gmail.com> wrote:
>
> I'm terribly confused on how to use try catch to continue execution within
> a loop
>
> I take, say, the data for 150 symbols from yahoo, and seek to parse them
> and append to existing index files in csv format. Sometimes the data is
> bad, the code pukes inside the loop and execution stops, as evident here:
>
> [109] "^BVSP - 20140324,47381.98,48141.789,47381.98,47993.422,0"
> [110] "^GSPTSE - 20140324,14335.76,14402.49,14226.88,14278.55,163174560"
> [111] "^N225 - 20140324,0,0,0,14475.3,0"
> [112] "CAC4L.NX - 20140324,N/A,N/A,N/A,0,N/A"
> [113] "^BSESN - 20140324,N/A,N/A,N/A,0,N/A"
> Error in value[[3L]](cond) : unused argument(s) (cond)
> Calls: tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
> In addition: Warning message:
> In if (file == "") file <- stdout() else if (is.character(file)) { :
>   the condition has length > 1 and only the first element will be used
> Execution halted
>
> Below is the code I am using, where i use tryCatch() to continue execution
> within the loop, but for whatever reason, it still breaks out of the loop
> on an error. Can someone please point out the error of my ways here? Thanks
> , Ralph Vince
>
> j <- "http://finance.yahoo.com/d/quotes.csv?s=";
> for (i in 1:150) {
>     j <- paste(j,brsym[[i]],sep="+");
> }
> j <- paste(j,"&f=sd1ohgl1vqd",sep="");
> print(j);
> X <- read.csv(j, header=FALSE,as.is=TRUE);
> #print(X);
> for (i in 1:150) {
>     tryCatch({
>         d<-strsplit(X[i,2], '/');
>         dd<-d[[1]][3];
>         if (as.numeric(d[[1]][1]) < 10){
>           dd<-paste(dd,0,sep="");
>         }
>         dd<-paste(dd,d[[1]][1],sep="");
>         if (as.numeric(d[[1]][2]) < 10){
>           dd<-paste(dd,0,sep="");
>         }
>         dd<-paste(dd,d[[1]][2],sep="");
>         dd<-paste(dd,X[i,3],sep=",");
>         dd<-paste(dd,X[i,4],sep=",");
>         dd<-paste(dd,X[i,5],sep=",");
>         dd<-paste(dd,X[i,6],sep=",");
>         dd<-paste(dd,X[i,7],sep=",");
>         ko <- paste(X[i,1], "-",dd);
>         print(ko);
>
>         k <- paste("/home/rvince/AsciiCsv/ES50/", X[i,1], sep="");
>         k <- trim.whitespace(k);
>         k <- paste(k,".csv", sep="");
>         write.table(dd, k, append = TRUE, quote = FALSE, sep = ",",
>                     eol = "\n", na = "0", dec = ".", row.names = FALSE,
>                     col.names = FALSE, qmethod = c("escape", "double"));
>     }, error = function() next)
> }
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.



More information about the R-SIG-Finance mailing list