r - 我在尝试使用 R 下载文件时遇到问题
问题描述
所以我想尝试一下 Covid 数据分析。我正在尝试复制我在这里读到的东西。
但我从一开始就有麻烦:
下载它使用的数据
## source data files
filenames <- c('time_series_covid19_confirmed_global.csv',
'time_series_covid19_deaths_global.csv',
'time_series_covid19_recovered_global.csv')
url.path <- paste0('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/',
'master/csse_covid_19_data/csse_covid_19_time_series/')
## download files to local
download <- function(filename) {
url <- file.path(url.path, filename)
dest <- file.path('./data', filename)
download.file(url, dest)
}
bin <- lapply(filenames, download)
## load data into R
raw.data.confirmed <- read.csv('./data/time_series_covid19_confirmed_global.csv')
raw.data.deaths <- read.csv('./data/time_series_covid19_deaths_global.csv')
raw.data.recovered <- read.csv('./data/time_series_covid19_recovered_global.csv')
运行此代码给我以下错误:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file './data/time_series_covid19_confirmed_global.csv': No such file or directory
现在我探索了两个方向:
- 我认为该链接可能已过时,因此我进行了一些更改,但出现了相同的错误,以转到此页面:
## fichiers sources filenames <- c("10-31-2020.csv") url.path <- paste0("https://github.com/CSSEGISandData/COVID-19", "blob/master/csse_covid_19_data/csse_covid_19_daily_reports") ## download files to local download <- function(filename) { url <- file.path(url.path, filename) dest <- file.path('./data', filename) download.file(url, dest) } bin <- lapply(filenames, download)
但我得到另一个错误:
Error in download.file(url, dest) :
cannot open destfile './data/10-31-2020.csv', reason 'No such file or directory'
- 现在我认为这是一个
dest
问题,并data
在我的工作目录中创建了一个文件夹,它有效地改变了我的错误,但仍然给了我一个:
trying URL 'https://github.com/CSSEGISandData/COVID-19blob/master/csse_covid_19_data/csse_covid_19_daily_reports/10-31-2020.csv' Error in download.file(url, dest) : cannot open URL 'https://github.com/CSSEGISandData/COVID-19blob/master/csse_covid_19_data/csse_covid_19_daily_reports/10-31-2020.csv' In addition: Warning message: In download.file(url, dest) : Error in download.file(url, dest) : cannot open URL 'https://github.com/CSSEGISandData/COVID-19blob/master/csse_covid_19_data/csse_covid_19_daily_reports/10-31-2020.csv'
现在我不确定为什么它会改变错误,因为我认为dest
indownload(url,dest)
是本地保存,但不是一个硬的,我更不确定接下来要检查什么。
我愿意接受任何其他更安全/更可靠或可复制的方式来下载此文件。我只是想要一种方法来自动化每天获取新文件的事实(从这里)
解决方案
dest
您的文件路径中需要一个斜杠:
dest <- file.path('./data/', filename)
推荐阅读
- python - google cloud function python CORS error 请求的资源上不存在“Access-Control-Allow-Origin”标头。
- android - 为什么 RecyclerViewAdapter DummyContent 元素和方法是静态的?
- go - 构建一个多可执行的 Go 项目
- javascript - 表元素使用 javascript 连接过早关闭
- php - php的解释器。阅读、翻译和执行源代码的指令?
- css - 使 Angular 应用程序中的背景图像加载速度更快
- arrays - 如何获得3个值的打印?
- c - 如何确保`strncmp`不超出它应该的范围?
- r - 如何使用 %in% 在 R 中进行字符串替换(str_replace)
- arrays - 将 int 写入值大于 255 的字节