2021年5月1日星期六

Accessing API with for-loop randomly has encoding error, which breaks loop in R

I'm trying to access an API from iNaturalist to download some citizen science data. I'm using the package rinat to get this done (see vignette). The loop below is, essentially, pulling all observations for one species, in one state, in one year iteratively on a per-month basis, then summing the number of observations for that year (input parameters subsetted from my actual script for convenience).

require(rinat)    state_ids <- c(18, 14)  bird_ids <- c(14886,1409)    months <- c(1:12)    final_nums <- vector()  for(i in 1:length(state_ids)){    total_count <- vector()        for(j in 1:length(months)){          monthly <- get_inat_obs(place_id=state_ids[i],                            taxon_id=bird_ids[i],                            year=2019,                            month = months[j])        total_count <- append(total, length(monthly$scientific_name))        print(paste("done with month", months[j], "in state", state_ids[i]))    }        final_nums <- append(final_nums, sum(total_count))    print(paste("done with state", state_ids[i]))  }  

Occasionally, and seemingly randomly, I get the following error:

No encoding supplied: defaulting to UTF-8.  Error in if (!x$headers$`content-type` == "text/csv; charset=utf-8") { :     argument is of length zero  

This ends up breaking the loop or makes the loop run without actually pulling any real data. Is this an issue with my script, or the API, or something else? I've tried manually supplying encoding information to the get_inat_obs() function, but it doesn't accept that as an argument. Thank you in advance!

https://stackoverflow.com/questions/67351337/accessing-api-with-for-loop-randomly-has-encoding-error-which-breaks-loop-in-r May 02, 2021 at 06:20AM

没有评论:

发表评论