[R] halting with errors

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Sep 18 18:58:41 CEST 2023


You can use try() or tryCatch().  The former is simpler, the latter is 
more flexible.

For example:

   result <- 
try(read_html(“https://timeseries.sepa.org.uk/KiWIS/KiWIS?service=kisters&type=queryServices&datasource=0&request=getTimeseriesValues&ts_path=1/15016/Q/15m.Cmd&from=2015-01-01&to=2015-01-31&returnfields=Timestamp,Value,Quality%20Code”))
   if (inherits(result, "try-error"))
     message("year 2015 doesn't have data")
   else {
      ... do the usual stuff ...
   }



On 18/09/2023 12:54 p.m., Nick Wray wrote:
> Hello  I am downloading flow data from the UK national river flow archive
> (NRFA).  I have code which works (thanks to previous help on r-help) but
> the problem is that before I make a call for the data for that particular
> year I don’t know whether the data exists for that particular year and
> catchment
> 
> This is a typical url  and it works if you paste it into the top line on a
> web page.  15007 is the code for the Pitnacree catchment in Scotland
> 
> https://timeseries.sepa.org.uk/KiWIS/KiWIS?service=kisters&type=queryServices&datasource=0&request=getTimeseriesValues&ts_path=1/15007/Q/15m.Cmd&from=2015-01-01&to=2015-01-31&returnfields=Timestamp,Value,Quality%20Code
> 
> It works in R as well with
> 
> page<-read_html("
> https://timeseries.sepa.org.uk/KiWIS/KiWIS?service=kisters&type=queryServices&datasource=0&request=getTimeseriesValues&ts_path=1/15007/Q/15m.Cmd&from=2015-01-01&to=2015-01-31&returnfields=Timestamp,Value,Quality%20Code
> ")
> 
>      yiq<-page |>
> 
>        html_element("table") |>
> 
>        html_table(header = TRUE) |>
> 
>        (\(x) {
> 
>          hdr <- unlist(x[3, ])
> 
>          y <- x[-(1:3), ]
> 
>          names(y) <- hdr
> 
>          y
> 
>        })()
> 
>          print(nrow(yiq))
> 
>      yiq
> 
> 
> 
> But if I try to move onto another catchment Kenmore 15006
> 
> eg
> 
> 
> 
> https://timeseries.sepa.org.uk/KiWIS/KiWIS?service=kisters&type=queryServices&datasource=0&request=getTimeseriesValues&ts_path=1/15016/Q/15m.Cmd&from=2015-01-01&to=2015-01-31&returnfields=Timestamp,Value,Quality%20Code
> 
> So in R
> 
> read_html(“
> https://timeseries.sepa.org.uk/KiWIS/KiWIS?service=kisters&type=queryServices&datasource=0&request=getTimeseriesValues&ts_path=1/15016/Q/15m.Cmd&from=2015-01-01&to=2015-01-31&returnfields=Timestamp,Value,Quality%20Code
> ”)
> 
> This doesn’t work because there’s no data for this catchment for this year
> 
> I am pasting in different years (from say 1961 to 2017) in a loop (and this
> works if the data sets exist) but the problem is that if the data is not
> there (and there doesn’t seem to be a way of determining this elsewhere)
> and so there’s nothing to read, an error message comes up and halts the
> program loop, so that manually I have to reset the url to try to find out
> whether there is data for the next year.
> 
> What I’d like to know is whether there’s any way in R of seeing whether the
> data set exists, and if doesn’t, moving on the next possibility without
> halting…
> 
> Any thoughts appreciated
> 
> Thanks Nick Wray
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list