当使用`lapply`或`map`

时间:2019-03-05 19:42:18

标签: r

尝试下载一些网站时,我仍然遇到相同的错误。

错误:

Warning messages: 1: In download.file(x, destfile = paste0("D:/temp/", filing_info$accession_number, : only first element of 'destfile' argument used

我正在尝试使用mapapply函数获取网址并下载内容并保存

数据:

URL:

url <- c("https://www.sec.gov/Archives/edgar/data/1750/000104746918004978/0001047469-18-004978-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746917004528/0001047469-17-004528-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746916014299/0001047469-16-014299-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746915006136/0001047469-15-006136-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746914006243/0001047469-14-006243-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746913007797/0001047469-13-007797-index.htm"
)

ID:

IDS <- c("0001047469-18-004978", "0001047469-17-004528", "0001047469-16-014299", 
"0001047469-15-006136", "0001047469-14-006243", "0001047469-13-007797"
)

代码:

library(purrr)
Map(function(x) {
  download.file(x, destfile = paste0("D:/temp/", IDS, ".htm"), quiet = FALSE)
}, url)

其他尝试:

map( url, function(x) {
  download.file(x, destfile = paste0("D:/temp/", IDS, ".htm"), quiet = FALSE)
})

lapply( url, function(x) {
  download.file(x, destfile = paste0("D:/temp/", IDS, ".htm"), quiet = FALSE)
})

sapply( url, function(x) {
  download.file(x, destfile = paste0("D:/temp/", IDS, ".htm"), quiet = FALSE)
})

编辑:这是我的数据的更好表示形式

data <- structure(list(links = structure(6:1, .Label = c("https://www.sec.gov/Archives/edgar/data/1750/000104746913007797/0001047469-13-007797-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746914006243/0001047469-14-006243-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746915006136/0001047469-15-006136-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746916014299/0001047469-16-014299-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746917004528/0001047469-17-004528-index.htm", 
"https://www.sec.gov/Archives/edgar/data/1750/000104746918004978/0001047469-18-004978-index.htm"
), class = "factor"), IDS = structure(6:1, .Label = c("0001047469-13-007797", 
"0001047469-14-006243", "0001047469-15-006136", "0001047469-16-014299", 
"0001047469-17-004528", "0001047469-18-004978"), class = "factor")), class = "data.frame", row.names = c(NA, 
-6L))

代码如下:

map(data$links, function(x) {
  download.file(x, destfile = paste0("D:/temp/", data$IDS, ".htm"), quiet = FALSE)
})

1 个答案:

答案 0 :(得分:2)

在这种情况下,您需要遍历ID和链接。为了迭代两个列表,请使用map2。实际上,由于您并没有真正返回值,而只是因为它的副作用而调用它,因此您可以使用walk2而不是map2

walk2(data$links, data$IDS, function(link, id) {
  download.file(link, destfile = paste0("D:/temp/", id, ".htm"), quiet = FALSE)
})