gpt4 book ai didi

r - download.file 错误 : scheme not supported

转载 作者:行者123 更新时间:2023-12-02 20:44:53 25 4
gpt4 key购买 nike

我需要从“http://www.elections.state.md.us”下载一些 csv 文件。

这是我的代码。

url <- "http://www.elections.state.md.us/elections/2012/election_data/index.html"
# recognize the links
links <- getHTMLLinks(url)
filenames <- links[str_detect(links,"_General.csv")]
filenames_list <- as.list(filenames)
filenames
# create a function
downloadcsv <- function(filename,baseurl,folder){
dir.create(folder,showWarnings = FALSE)
fileurl <- str_c(baseurl,filename)
if(!file.exists(str_c(folder,"/",filename))){
download.file(fileurl,
destfile = str_c(folder,"/",filename))
# 1 sec delay between files
Sys.sleep(1)
}
}
library(plyr)
l_ply(filenames_list,downloadcsv,
baseurl = "www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")

错误如下:

Error in download.file(fileurl, destfile = str_c(folder, "/", filename)) : scheme not supported in URL 'www.elections.state.md.us/elections/2012/election_data/State_Congressional_Districts_2012_General.csv'

但是,当我尝试将 url 粘贴到 IE 中时,它确实起作用了。那么我的代码有什么问题呢?

任何想法都会有所帮助,谢谢。

最佳答案

事实证明,url 必须以 http://、https://、ftp://或 file://等方案开头。所以在最后一行,我把代码改成了

l_ply(filenames_list,downloadcsv,
baseurl = "http://www.elections.state.md.us/elections/2012/election_data/",
folder = "elec12_maryland")

而且它有效。

关于r - download.file 错误 : scheme not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com