Anthony Martin
Anthony Martin

Reputation: 787

I am having trouble trying to download files with R

So I am trying to play a bit with Covid-data analysis. I am trying to reproduce things I read here.
But I have trouble from the beginning :

To download the data it uses

## 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')

Running this code gives me the following error :

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

Now I have explored two directions :

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

But I get another error :

 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'

Now I am not sure why it changes the error, as I thought the dest in download(url,dest) was a local save, but not a hard one And I am even more unsure what to check next.

I am open to any other safer/more reliable or reproductible way to download this file. I just want a way to automate the fact to get the new file each day (from here)

Upvotes: 0

Views: 859

Answers (1)

semaphorism
semaphorism

Reputation: 866

You need a trailing slash in your dest file path:

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

Upvotes: 1

Related Questions