rtidyversesleep

How to introduce a lag (sleep) in rowwise mutate


I need to make a call to an API separately for each row of my tibble. How can I introduce a small lag between each call? I need to do this because the API I am using limits the allowable number of requests per second.

How can I modify the following (psuedo) code to accomplish this? The code creates a list column called authors, which is populated by the result from the (made up) get_API_value API call.

library(tidyverse)
data %>% 
    rowwise() %>%
    mutate(authors = list(get_API_value(arg1 = val1, arg2 = val2)))

In other words, how can I make the above code incorporate a lag (such as Sys.sleep(1))?


Solution

  • You could add a Sys.sleep() inside of curly braces, separated by a semi-colon

    library(tidyverse)
    data %>% 
        rowwise() %>%
        mutate(authors = {Sys.sleep(1); list(get_API_value(arg1 = val1, arg2 = val2))})