Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R multiple urls into lapply

Tags:

function

r

lapply

I have a list of urls in a character vector and I want to pause the process between queries because if not the x queries is rejected.

urls=c('url1','url2','url3')

here is want I want to do

htmlpages=lapply(urls,function(x) readLines(x) Sys.sleep(0.3))
like image 455
user3083330 Avatar asked Dec 09 '13 15:12

user3083330


1 Answers

As Justin suggested, formatting is the key.

htmlpages = lapply(
  urls,
  function(x) 
  {
    y <- readLines(x)
    Sys.sleep(0.3)
    y
  }
)

Update: functionality for waiting between calls is now built into purrr.

library(purrr)
slow_readLines <- slowly(readLines, rate = rate_delay(0.3))
lapply(urls, slow_readLines)

or for a full purrr solution

library(purrr)
slow_readLines <- slowly(readLines, rate = rate_delay(0.3))
urls %>% map(slow_readLines)
like image 91
Richie Cotton Avatar answered Sep 30 '22 18:09

Richie Cotton