#RStats — API calls and Sys.sleep

1 minute read

Lately, I received a mail concerning my blogpost on Discogs API, saying the code didn’t work. Turned out it was due to new API limitations. Here’s how to get along with it.

Discogs calls

Here is the blog post describing how to make calls on the Discogs API with R.

I’ve recently received a mail saying the second part of the code wasn’t working, returning only NA. It turned out it was due to recent changes in Discogs API policy, limiting the number of calls you can make within a specific time window.

Also recently, I’ve been using Microsoft’s Computer Vision API — an API limiting calls to 20 per minute — for a blogpost on Data Bzh. So I said to myself: “time for a new blogpost!”.

How can you automate your API calls when the API is time-limited (for example, if you have 282 calls to make)?

Simplest way : a for loop and Sys.sleep()

Note : I won’t be making any API calls within this post, I’ll use Sys.time() to show how Sys.sleep() works.

If you’re calling Sys.time, and want to limit to 20 calls per minute, you’ll need to use Sys.sleep(). This function only takes one argument, time, which is the number of seconds you want R to stop before resuming.

When run, this function pause your session during the number of seconds you’ve entered. If you use this in a for loop, you can make a pause on every iteration of the loop. Here is an example with 10 seconds :

for(i in 1:3){
  Sys.sleep(time = 10)
## [1] "2017-03-26 11:13:58 CET"
## [1] "2017-03-26 11:14:08 CET"
## [1] "2017-03-26 11:14:18 CET"

With a lapply

If you have access to the core of the function you want to use (i.e. the function calling on the API), you can use a lapply and insert a Sys.sleep() staight inside this function.

This is the method you’ll need to use if you’re trying to replicate the Discogs API calls.

lapply(1:3, function(x) {
}) %>% do.call(rbind, .) 
## [1] 1
## [1] "2017-03-26 11:20:22 CET"
## [1] 2
## [1] "2017-03-26 11:20:25 CET"
## [1] 3
## [1] "2017-03-26 11:20:28 CET"

Hope this can help!