在 R 中使用应用函数的嵌套循环

Nested Loop using apply functions in R

我有以下功能可以使用 RSelinium 和 phantomjs 从网站获取一些 url。

get_url <- function(url){
  rdr$navigate(url)
  li <- rdr$findElements(using = 'xpath',  "//div[@data-id]")
  str <- sapply(li, function(x){x$getElementAttribute('outerHTML')})
  if(length(str)>1){
  tree <- htmlParse(str)
  url <- getNodeSet(tree, '//div//a[@class="link url"]')
  url <- sapply(url, xmlGetAttr, 'href')
  }
}

并且 url 存储在 30 x 60 矩阵中。

我尝试使用以下嵌套循环来执行此操作。

for(i in 1:ncol(offset_url)){
  for(j in 1:nrow(offset_url)){
    url_list <- rbind(url_list,get_url(offset_url[j,i]))
  }
}

但是执行起来比较费时间

有什么方法可以使用应用函数来减少时间吗?

这有帮助吗?

do.call(rbind,list(mapply(function(x,y) get_url(offset_url[x,y]),x=row(offset_url),y=col(offset_url))))