2
votes

I am using the function POST from httr inside the function possibly from purrr to download multiples pdfs from a vector of URLs so I can skip to the next URL if any error happens. The problem is that I need to add a Sys.sleep from one download to the next. I know how to do it with for loop, but I can't figure out how to do it with the package purrr. For example, with for loop, I would do it this way:

df<-data.frame(id=1:4,url=c("url1","url2","url3","url4"))
for (i in 1:4){
POST(df$url[i],body=body,write_disk(paste0("df$id[i]",".pdf"))
Sys.sleep(1)
        }

How can use something equivalent to Sys.sleep with the purrr package?

1
Do you already have this code in purrr without the Sys.sleep()?cirofdo
Maybe walk2(df$id, df$url, ~{possibly(POST, NULL)(.y, body = body, write_disk(paste0(.x, ".pdf"))); Sys.sleep(1)})alistaire

1 Answers

2
votes

I didn't immediately follow what was happening in alistaire's answer above, so just to be totally explicit,

you can pass map a multi-line expression as an anonymous function using ~{}, as in:

map(1:3, ~ {
  Sys.sleep(10)
  cat(.x)
  .x
})

h/t Emil Hvitfeldt