我尝试使用R

问题描述

因此,我尝试使用Covid数据分析。我正在尝试复制事物I read here
但是我从一开始就有麻烦:

要下载其使用的数据

## 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,download)

但是我得到另一个错误

 Error in download.file(url,dest) : 
  cannot open destfile './data/10-31-2020.csv',reason 'No such file or directory'
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中的download(url,dest)是本地保存,但不是硬保存 而且我甚至不确定下一步要检查什么。

我愿意以其他更安全/更可靠或可复制的方式下载此文件。 我只想要一种自动事实(每天从here获取文件的方式)

解决方法

您的dest文件路径中需要用斜杠结尾:

dest <- file.path('./data/',filename)