cole cole - 3 months ago 21
R Question

Retry element of a list with tryCatch in lapply

I'm querying data from GA where the start date for each iteration is contained in a list (begDate). I have researched how to stop the loss of the queried data using tryCatch if an error occurs before lapply cycles through the list.
Usually, the errors occur because of timeouts. I'd like to be able to retry the element before moving onto the next element. To do this I have put in the error handler the same code as in the main body of tryCatch. However, this is no good when the query in the error handler times out as well. How do I write this properly? Most posts I have found deal with this issue in a loop or move on to the next element. I looked at withRestarts as well but did not understand how to use it.

begDate<-array(c(seq(as.Date("2014-04-01"), as.Date("2014-04-03"), by="1 day"))) #defining start date for each query

Test<-lapply(begDate, function(bDt){ #uses each element of begDate as the beginning time period of query with 90 day length
rg2<-paste0("users::condition::ga:sessionCount==1;dateOfSession<>", as.Date(bDt, origin = "1970-01-01"), "_", as.Date(bDt, origin = "1970-01-01"), ";users::condition::ga:goalCompletionsAll>0" )



tryCatch( ga$getData(id,
walk = TRUE,
batch = TRUE,
start.date = as.Date(bDt+1, origin = "1970-01-01"),
end.date = as.Date(bDt+15, origin = "1970-01-01"),
metrics = "ga:sessions, ga:goalCompletionsAll" ,
dimensions = "ga:date, ga:goalPreviousStep1, ga:goalPreviousStep2, ga:goalPreviousStep3",
segment = paste0(rg2),
filters = ""
)
, error=function(e) { # just retries the query
ga$getData(id,
walk = TRUE,
batch = TRUE,
start.date = as.Date(bDt+1, origin = "1970-01-01"),
end.date = as.Date(bDt+15, origin = "1970-01-01"),
metrics = "ga:sessions, ga:goalCompletionsAll" ,
dimensions = "ga:date, ga:goalPreviousStep1, ga:goalPreviousStep2, ga:goalPreviousStep3",
segment = paste0(rg2),
filters = ""
)
}

)

})

Answer

This is the solution I have found to be most stable in the absence of knowing how to write better code (which I am constantly trying to improve upon).

Test<-lapply(begDate, function(bDt){ #uses each element of begDate as the beginning time period of query with 50 day length
  rg2<-paste0("users::condition::ga:source=~", urlV, ";dateOfSession<>", as.Date(bDt, origin = "1970-01-01"), "_", as.Date(bDt, origin = "1970-01-01") )

tryCatch( ga$getData(id, walk = TRUE, batch = TRUE, start.date = as.Date(bDt+1, origin = "1970-01-01"), end.date = as.Date(bDt+50, origin = "1970-01-01"), metrics = paste0(metr) , dimensions = paste0(dim), segment = seg, filters = "ga:country=~United States" )

, error=function(e) { Sys.sleep(7) tryCatch( ga$getData(id, walk = TRUE, batch = TRUE, start.date = as.Date(bDt+1, origin = "1970-01-01"), end.date = as.Date(bDt+50, origin = "1970-01-01"), metrics = paste0(metr) , dimensions = paste0(dim), segment = seg, filters = "ga:country=~United States" ) , error = function(e) print(paste0(bDt, " ", e))) })

})