diff --git a/R/getGeneralWQPData.R b/R/getGeneralWQPData.R index f163f623a7bdd04b8d148b918c04013b14f662de..3e7eb95f47316200c8cf590a0a79a4f1ed01bcf0 100644 --- a/R/getGeneralWQPData.R +++ b/R/getGeneralWQPData.R @@ -31,6 +31,44 @@ getGeneralWQPData <- function(...){ urlCall, "&mimeType=tsv",sep = "") - suppressWarnings(retval <- read.delim(urlCall, header = TRUE, quote="\"", dec=".", sep='\t', colClasses=c('character'), fill = TRUE)) - return(retval) + doc = tryCatch({ + h <- basicHeaderGatherer() + doc <- getURL(urlCall, headerfunction = h$update) + + }, warning = function(w) { + message(paste("URL caused a warning:", urlCall)) + message(w) + }, error = function(e) { + message(paste("URL does not seem to exist:", urlCall)) + message(e) + return(NA) + }) + + if(h$value()["Content-Type"] == "text/tab-separated-values;charset=UTF-8"){ + + numToBeReturned <- as.numeric(h$value()["Total-Result-Count"]) + + if (!is.na(numToBeReturned) | numToBeReturned != 0){ + retval <- read.delim(textConnection(doc), header = TRUE, quote="\"", + dec=".", sep='\t', + colClasses=c('character'), + fill = TRUE) + + actualNumReturned <- nrow(retval) + + if(actualNumReturned != numToBeReturned) warning(numToBeReturned, " sample results were expected, ", actualNumReturned, " were returned") + + return(retval) + } else { + warning(paste("No data to retrieve from",urlCall)) + return(NA) + } + + } else { + message(paste("URL caused an error:", urlCall)) + message("Content-Type=",h$value()["Content-Type"]) + return(NA) + } + + } \ No newline at end of file